selectdistinct S.SNAME, P.PNAME from S,SPJ,P where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO
求只向与自己位于不同城市的工程供应零件的供应商姓名
1 2 3 4 5 6 7
select SNAME from S where SNAME notin ( select SNAME from S,SPJ,J where S.SNO = SPJ.SNO and SPJ.JNO = J.JNO and S.CITY = J.CITY )
求只向与自己位于不同城市的工程供应零件的供应商姓名
1 2 3 4 5 6 7
select SNAME from S where SNAME notin ( select SNAME from S,SPJ,J where S.SNO = SPJ.SNO and SPJ.JNO = J.JNO and S.CITY != J.CITY )
求供应了所有零件的供应商姓名
不存在一个零件,所求的供应商没有供应
1 2 3 4 5 6 7 8 9 10 11
select SNAME from S wherenotexists ( select PNO /* 使用了*则不可能使用索引 */ from P wherenotexists ( select SNO /* 使用了*则不可能使用索引 */ from SPJ where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO ) )
求供应了所有红色零件的供应商姓名
1 2 3 4 5 6 7 8 9 10 11
select SNAME from S wherenotexists ( select PNO from P where P.COLOR ='红色'andnotexists ( select SNO from SPJ where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO ) )
列出每个城市的工程所使用的零件总的数量
left join:可能有 0 个
1 2 3
select CITY, sum(QTY) from J leftjoin SPJ on J.JNO = SPJ.JNO groupby CITY
select SPJ.JNO, SPJ.PNO, sum(QTY*PRICE) from J,SPJ,P where J.JNO = SPJ.JNO and SPJ.PNO = P.PNO and P.COLOR ='红色' groupby SPJ.JNO, SPJ.PNO orderbysum(QTY) desc
求供应零件数量最多的供应商姓名
1 2 3 4 5 6 7 8 9 10
select SNAME from S,SPJ,P where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO groupby SPJ.SNO havingsum(QTY) >=all ( selectsum(QTY) from S,SPJ,P where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO groupby SPJ.SNO )
求每个城市中供应零件数量最多的供应商姓名。
1 2 3 4 5 6 7 8 9 10
select S1.CITY, SNAME from S S1,SPJ,P where S1.SNO = SPJ.SNO and SPJ.PNO = P.PNO groupby SPJ.SNO, S1.CITY havingsum(QTY) >=all ( selectsum(QTY) from S,SPJ,P where S.SNO = SPJ.SNO and SPJ.PNO = P.PNO and S.CITY = S1.CITY groupby SPJ.SNO )