繁体   English   中英

如何为此编写SQL查询:

[英]How to write sql query for this:

mysql> select * from table3 order by id;
+------+-------+
| id   | value |
+------+-------+
|    1 | a     |
|    1 | b     |
|    1 | c     |
|    1 | d     |
|    2 | a     |
|    2 | b     |
|    3 | a     |
|    3 | b     |
|    3 | c     |
|    4 | a     |
|    4 | b     |
+------+-------+

我想选择所有没有值'c'的ID

不能通过以下查询简单地工作:

mysql> select distinct id from table3 where value <> 'c';
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
+------+

我所需要的只是2和4的回报。

感谢您的关注!

这将为您工作

select distinct id from table3 where 
             id not in ( select id from table3 where value = 'c')
select distinct id 
from table3 t
where not exists 
                 (select 1 from table3 where value = 'c' and id = t.id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM