Ashutosh SIngh
Ashutosh SIngh

Reputation: 1019

Where not exists in mysql How ignore all id if exist with some condition

My question is if ID uses codes FREE25 , FREE20 and FREE10 I want to ignore all IDs those who use codes FREE25 and FREE20.

How ignore all IDs if ID present has codes with FREE25 , FREE20?

+------+--------+
| ID   | Code   |
+------+--------+
|    1 | FREE25 |
|    1 | FREE20 |
|    1 | FREE10 |
|    2 | FREE10 |
|    3 | FREE10 |
|    3 | FREE50 |
+------+--------+

I want output like

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

Upvotes: 1

Views: 153

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You may use 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')
);

Upvotes: 1

Related Questions