Sorry for the title I just do not know how to explain what I want to achieve briefly. But it is actually simple.
I have the following table egr
:
+---------+------------+
| offid | groupid |
+---------+------------+
| 1 | 101 |
| 1 | 202 |
| 2 | 202 |
| 2 | 404 |
+---------+------------+
I would like to get the rows for which a groupid is not linked to the other offid. Result would be:
+---------+------------+
| offid | groupid |
+---------+------------+
| 1 | 101 |
| 2 | 404 |
+---------+------------+
This works but I was wondering if there was a more elegant way to do it?
select * from egr as egr1
where egr1.offid = 1
and egr1.groupid not in (select groupid from egr as egr2 where egr2.offid = 2 and egr1.groupid = egr2.groupid)
union
select * from egr as egr1
where egr1.offid = 2
and egr1.groupid not in (select groupid from egr as egr2 where egr2.offid = 1 and egr1.groupid = egr2.groupid)
In case you would like to try:
create table egr (offid int, groupid int);
insert into egr values (1, 101), (1, 202), (2, 202), (2, 404);
Thanks