mysql - 影响单行内的多个(或所有)列的单个条件检查
问题描述
我想要完成的是影响所有行的多个列,例如column1 > 5
更改或替换它们的值。
是否有任何优雅的方法可以执行一次条件检查(假设它是一个复杂的语句)并将其结果用于使用的列CASE / WHEN
?
我知道我可以使用WHERE column1 > 5
and UNION
of sorts 执行标准过滤,但我想了解是否有任何其他选项(甚至可能是特定于 MySQL 的)可用。
编辑:
举个例子:
SELECT
CASE
WHEN Country = 'UK' THEN CustomerId
ELSE '???'
END AS "CustomerId",
CASE
WHEN Country = 'UK' THEN City
ELSE '???'
END AS "City",
CASE
WHEN Country = 'UK' THEN Country
ELSE '???'
END AS "Country"
FROM Customers
如您所见,我在CASE WHEN Country = 'UK'
这里多次使用,我想避免。
解决方案
您可以使用union all
:
select c.customerid, c.city, c.country
from customers c
where country = 'UK'
union all
select '???', '???', '???'
from customers c
where country is null or country <> 'UK'
推荐阅读
- c++ - 模板参数是模棱两可的/推断的参数冲突类型
- android - 在 shrinkResources 之后 GeoNames 和 JDOM 请求 java.rmi.RemoteException
- arrays - 为什么我不能将我的自定义对象附加到数组?
- python - 在 mac osx 10.15.2 上安装 Psycopg2
- python - 使用 Python 问题发布到谷歌表单
- java - 我的 hibernate.cfg.xml 文件中有错误
- python - Python:使用一个 sqlite 查询来查找 NOT EXISTS 结果
- ruby - 如何运行“bundle exec jekyll new”。
- apache-spark - Spark Streaming Direct Approach 中的 Exactly once 语义
- c# - c#如果匹配则返回值,否则继续