Reputation: 13
tbl_pack_service;
+-------+---------+------------+
| ps_id | pack_id | service_id |
+-------+---------+------------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 2 | 4 |
| 7 | 2 | 5 |
| 8 | 3 | 1 |
| 9 | 3 | 2 |
| 10 | 3 | 3 |
| 11 | 3 | 4 |
+-------+---------+------------+
ps_id is primary key i am tying to make dynamic select list in php i want a mysql query that can give service id which do not match with particular pack_id
like i have service_id 1,2,3,4,5 when i should select pack_id=1 then 3,4,5 should be displayed and when i should select pack_id=2 then nothing should be displayed as it has all the 5 services. thanks..
Upvotes: 1
Views: 154
Reputation: 270795
There are a few ways to handle this. The easiest is with a NOT IN
subquery:
SELECT DISTINCT service_id
FROM
tbl_pack_service
WHERE service_id NOT IN (SELECT service_id FROM tbl_pack_service WHERE pack_id = 1)
Upvotes: 3