我有以下关系和
1.我想查找每个供应商提供的零件的pids并且小于200。(如果任何供应商不是简单的零件或收费超过200,则该零件未被选中)
小提琴http://sqlfiddle.com/#!2/4b5d4
Supplies
sid 1 2 3 4 5
sname Jason David John Peter Jay
address 221 2b 3c 4d 5e
Parts
pid 10 20 30 40 50 60
pname Head Body Hand Leg Arm Foot
color red blue green white red green
Catalog
sid 1 1 2 2 3 4 1 1 4 4 1 5 5 3 3 2
pid 10 20 20 30 30 40 30 40 10 50 50 50 10 60 10 10
cost 150 220 150 150 130 125 130 280 123 126 120 100 100 210 100 50
所以我做了以下查询,我认为它等于there is no parts that the suppliers don't supply it.
SELECT C.pid
FROM CATALOG C
WHERE C.cost < 200
AND NOT EXISTS (SELECT S.sid
FROM Suppliers S
WHERE NOT EXISTS
(SELECT P.pid
FROM Parts P
WHERE P.pid = C.pid
AND S.sid = C.sid ))
但它返回 null,它应该返回 10。
2.我想为每个供应绿色部分和红色部分的供应商找到,打印出她供应的最昂贵部分的名称和价格。
我只能找出绿色和红色的部分,但找不到最贵的。
问题是我已经尝试过了。
SELECT S.sname
FROM Suppliers AS S,
Parts AS P1,
CATALOG AS C1,
Parts AS P2 ,
CATALOG AS C2
WHERE S.sid = C1.sid
AND C1.pid = P1.pid
AND S.sid = C2.sid
AND C2.pid = P2.pid
AND ( P1.color = 'red'
AND P2.color = 'green' )
我如何解决它?预先感谢。
ps 对不起,我要去上课,我会在大约 6 小时后回复。