Reputation: 57
I have a table where I store records for either a person or a group.
|id|entity_id|person_id|group_id|
|1 |000000001|999999999|NULL |
|2 |000000001|NULL |88888888|
|3 |000000002|999999999|NULL |
|4 |000000003|NULL |88888888|
How can I get all the records for person_id 999999999 and group_id 88888888, but the latter only when I do not have a record with entity_id 000000001 for person_id 999999999
So my result should be
|id|entity_id|person_id|group_id|
|1 |000000001|999999999|NULL |
|3 |000000002|999999999|NULL |
|4 |000000003|NULL |88888888|
I am hoping this is possible in one query, but UNION, EXCEPT do not seem to do the trick.
I was trying something like
SELECT *
from myTable m1
WHERE NOT IS NULL person_id
UNION ALL
SELECT *
from myTable m2
WHERE NOT IS NULL group_id
EXCEPT (SELECT entity_id FROM m1)
but apparently mysql doesn't support EXCEPT
Upvotes: 0
Views: 152