USE Homework250313
Go
-- 求供应工程 J1 零件的供应商号 SNO
SELECT DISTINCT SNO
FROM SPJ
WHERE JNO = 'J1';
Go
-- 求供应工程 J1 零件 P1 的供应商号 SNO
SELECT DISTINCT SNO
FROM SPJ
WHERE JNO = 'J1'
  AND PNO = 'P1';
Go
-- 求供应工程 J1 红色零件的供应商号 SNO
SELECT DISTINCT SPJ.SNO
FROM SPJ
         JOIN P ON SPJ.PNO = P.PNO
WHERE SPJ.JNO = 'J1'
  AND P.COLOR = N'红';
Go
-- 求没有使用天津供应商生产的红色零件的工程号 JNO
SELECT JNO
FROM J
WHERE JNO NOT IN (SELECT DISTINCT SPJ.JNO
                  FROM SPJ
                           JOIN S ON SPJ.SNO = S.SNO
                           JOIN P ON SPJ.PNO = P.PNO
                  WHERE S.CITY = N'天津'
                    AND P.COLOR = N'红');
Go
-- 求至少用了 S1 供应商所供应的全部零件的工程号 JNO
SELECT JNO
FROM SPJ
WHERE PNO IN (SELECT PNO FROM SPJ WHERE SNO = 'S1')
GROUP BY JNO
HAVING COUNT(DISTINCT PNO) = (SELECT COUNT(DISTINCT PNO) FROM SPJ WHERE SNO = 'S1');
Go
-- 找出所有供应商的姓名和所在城市
SELECT SNAME, CITY
FROM S;
Go
-- 找出所有零件的名称、颜色、重量
SELECT PNAME, COLOR, WEIGHT
FROM P;
Go
-- 找出使用供应商 S1 所供应零件的工程号码
SELECT DISTINCT JNO
FROM SPJ
WHERE SNO = 'S1';
Go