mysql中不存在的地方如果存在某些条件,如何忽略所有id

时间:2015-05-21 06:37:11

标签: mysql sql

我的问题是ID是否使用代码FREE25FREE20FREE10我想忽略使用代码FREE25FREE20的所有ID。< / p>

如果ID存在的代码包含FREE25FREE20

,如何忽略所有ID
+------+--------+
| ID   | Code   |
+------+--------+
|    1 | FREE25 |
|    1 | FREE20 |
|    1 | FREE10 |
|    2 | FREE10 |
|    3 | FREE10 |
|    3 | FREE50 |
+------+--------+

我想输出

+------+
| ID   |
+------+
|    2 | 
|    3 |
+------+

1 个答案:

答案 0 :(得分:1)

您可以使用not exists

select
distinct t1.id from table_name t1
where not exists (
 select 1 from table_name t2
 where t1.id = t2.id
 and t2.code in ('FREE25','FREE20')
);