[英]How can I select all non-unique field1s where field2 is never x?
給定以下數據集:
field1 field2
a 1
a 2
a 3
b 1
b 2
b 4
c 2
c 2
c 3
如何確定field1
哪些值從未與field2
值為4的表相關聯?
結果將是
field1
a
c
SELECT field1 FROM table WHERE field2 <> 4
將包含b,因為它與其他field2
值一起多次出現在表中-如何防止這種情況發生?
您可以group by field1
然后將條件放在group by field1
子句中:
select field1
from tablename
group by field1
having sum(field2 = 4) = 0
使用不存在
select t1.* from table t1
where not exists( select 1 from table t2 where t1.field1=t2.field1
and t2.field2=4)
或不用於
select * from table t1
where t1.field1 not in ( select field1 from table where field2=4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.