I have a simple table that contains references between items. In another table, the items are grouped together. Each item can be in any number of groups.
example:
Items:
ItemID | ItemName
------------------
1 | Item 1
2 | Item 2
3 | Item 3
4 | Item 4
Groups:
GroupID | ItemID
-----------------
1 | 1
1 | 2
1 | 4
2 | 2
2 | 3
References:
RefSourceItem | RefTargetItem
-------------------------------
1 | 2
2 | 3
2 | 4
Now I want to select all References in a certain group, that is, source AND target are both in the same group. The statement I came up with is this:
SELECT * FROM References
WHERE RefSourceItem IN (SELECT ItemID FROM Groups WHERE GroupID = 1)
AND RefTargetItem IN (SELECT ItemID FROM Groups WHERE GroupID = 1)
What I find a bit problematic is that I have two identical subselects.
My question:
Is there a better way to restrict two columns to the same subselect?
