MySQL, which you appear to be using, does not support the INTERSECT
syntax. You're going to have to solve it another way.
In this case, it is trivial -we only need a list of all suppliers that offer "green" and "red" of some part- your query does not bother to see if the parts themselves are related, so we can solve it quite easily like this:
SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color IN ('red', 'green')
GROUP BY Suppliers.sid
HAVING COUNT(DISTINCT Parts.color) = 2
Personally, I don't believe the original query is a typical INTERSECT
problem. Take a look at the JOIN
solution offered by Vinko Vrsalovic for a general solution to emulate the INTERSECT
(which I would btw prefer even if the RDBMS would in fact offer INTERSECT
natively).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…