Using group by and having clause

前端 未结 5 1279
一生所求
一生所求 2020-12-07 11:59

Using the following schema:

Supplier (sid, name, status, city)
Part (pid, name, color, weight, city)
Project (jid, name, city)
Supplies (sid, pid, jid**, qua         


        
5条回答
  •  醉酒成梦
    2020-12-07 12:27

    What type of sql database are using (MSSQL, Oracle etc)? I believe what you have written is correct.

    You could also write the first query like this:

    SELECT s.sid, s.name
    FROM Supplier s
    WHERE (SELECT COUNT(DISTINCT pr.jid)
           FROM Supplies su, Projects pr
           WHERE su.sid = s.sid 
               AND pr.jid = su.jid) >= 2
    

    It's a little more readable, and less mind-bending than trying to do it with GROUP BY. Performance may differ though.

提交回复
热议问题