[英]How can I select all non-unique field1s where field2 is never x?
Given the dataset below: 给定以下数据集:
field1 field2
a 1
a 2
a 3
b 1
b 2
b 4
c 2
c 2
c 3
How can I determine which values for field1
are never associated anywhere in the table with a field2
value of 4? 如何确定
field1
哪些值从未与field2
值为4的表相关联?
The result would be 结果将是
field1
a
c
SELECT field1 FROM table WHERE field2 <> 4
will include b because it appears in the table multiple times with other field2
values - how do I prevent this? SELECT field1 FROM table WHERE field2 <> 4
将包含b,因为它与其他field2
值一起多次出现在表中-如何防止这种情况发生?
You can group by field1
and put the condition in the having clause: 您可以
group by field1
然后将条件放在group by field1
子句中:
select field1
from tablename
group by field1
having sum(field2 = 4) = 0
use not exists 使用不存在
select t1.* from table t1
where not exists( select 1 from table t2 where t1.field1=t2.field1
and t2.field2=4)
or use not in 或不用于
select * from table t1
where t1.field1 not in ( select field1 from table where field2=4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.