SQL: Syntax error with intersect?

后端 未结 6 2070
北恋
北恋 2020-12-06 17:27

This is my query:

-- Sids of suppliers who supply a green part AND a red part
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.si         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 17:49

    Nothing, MySQL doesn't have the INTERSECT keyword. You can rewrite it as an INNER JOIN:

    SELECT DISTINCT sid FROM
    (SELECT Suppliers.sid
    FROM Suppliers
    JOIN Catalog ON Catalog.sid = Suppliers.sid
    JOIN Parts ON Parts.pid = Catalog.pid
    WHERE Parts.color = "red") a
    INNER JOIN
    (SELECT Suppliers.sid
    FROM Suppliers
    JOIN Catalog ON Catalog.sid = Suppliers.sid
    JOIN Parts ON Parts.pid = Catalog.pid
    WHERE Parts.color = "green") b
    ON (a.sid = b.sid);
    

    This query can surely be better written, but this is to show that intersect is but merely an inner join with a select distinct, you can automatically transform one into the other.

提交回复
热议问题