0
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回报。

感谢您的关注!

4

2 回答 2

2

这对你有用

select distinct id from table3 where 
             id not in ( select id from table3 where value = 'c')
于 2012-11-16T06:45:28.960 回答
1
select distinct id 
from table3 t
where not exists 
                 (select 1 from table3 where value = 'c' and id = t.id)
于 2012-11-16T06:45:55.900 回答