簡體   English   中英

如何選擇field2永遠不是x的所有非唯一field1?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM