col1 col2 A bear A dog A cat B bear B dog B cat C dog C cat D bear D dog D cat E bear E dog E cat F dog F cat
如果我想选择所有col1值至少有一行col2 =’bear’,我可以做:
SELECT col1 FROM mytable WHERE col1 IN ('A','B','C') GROUP BY col1 HAVING col2 = 'bear'
这将返回A和B
但是我想仅选择col1中没有一行的值,其中col2 =’bear’
我认为没有,但这似乎不起作用.
有任何想法吗?谢谢!
解决方法
SELECT m1.col1 FROM mytable m1 WHERE NOT EXISTS(SELECT NULL FROM mytable m2 WHERE m2.col1 = m1.col1 AND m2.col2 = 'bear') AND m1.col1 IN ('A','C')