可能是一个大标题!对此感到抱歉..:(
Table 1
id col1 col2
1 10 one
2 11 two
3 10 three
现在,我想编写一个sql查询来从table1中获取不同的col1,而table1中的col2中没有三个。我只需要输出 col1 - 11。
我尝试像这样从table1中选择不同的col1,其中col2!='三'
但这给出的结果是10和11。但是对于10,它有相应的行,其中3作为col2值。请帮我找到这个。
最佳答案
使用group by
和having
。
select col1
from table1
group by col
having sum(case when col2='three' then 1 else 0 end)=0
如果您使用 MySQL,having
条件可以缩短为
select col1
from table1
group by col
having sum(col2='three')=0
as 条件被视为 bool 值,返回 1 表示 true,0 表示 false。
关于mysql - 当表中有多个带有 col val1 的行时,如何从列值 2 中不包含特定值的表中获取不同的列 value1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43148503/