I have the following table:
---------------------------
| id | capital_id | value |
---------------------------
| 1 | 1 | a |
---------------------------
| 2 | 2 | b |
---------------------------
| 3 | 2 | c |
---------------------------
| 4 | 2 | d |
---------------------------
| 5 | 3 | b |
---------------------------
| 6 | 3 | e |
---------------------------
| 7 | 4 | f |
---------------------------
I need to select only distinct capital_id’s, but different from one that has a value given.
To be more clear, I’ll provide an example: If I have the record with id=5, I need to fetch all distinct capital_id’s, different than 3 and with the value different from ‘b’ (so capital_id’s to be fetched are: 1 and 4).
I managed to write the query like SELECT id FROM table WHERE capital_id != $capital_id AND value != $value, but duplicate capital_id’s are fetched this way. I tried to add a GROUP BY capital_id, but then capital_id=2 is also fetched, although one of its values is ‘b’.
How can I solve this problem?
1 Answer