我有一个包含 200 列的表,我想选择包含以下内容的所有列 “已完成”或“不完整”值。该表有 180 列,可以包含“已完成”或“不完整”值,其他列包括日期、签名、ID 等。
所以第一个想法是编写一个长查询,例如,
SELECT column2,column4,column5,...,column199 FROM table WHERE column2 like 'completed' or column2 like 'incomplete' or column4 like....
但是这个查询太简单而且太长。 有没有办法写像下面这样的东西?
select
[Columns that have values like 'completed' or 'incomplete'( if the value is not them do not select them)]
from Table
请有人帮忙或有任何想法,谢谢
最佳答案
对于这种特殊情况,您可以执行以下操作:
where greatest(col1, col2, col3) = 'completed' and
least(col4, col5, col6) = 'incompleted'
使用least()
和greatest()
是因为有两个值并且基于值的顺序。
我应该注意到数据结构是可疑的。每个状态有 180 行行会更好。
关于mysql - 如何提取Where子句中所有列的一个指定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794684/