0

我有以下关系和

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 小时后回复。

4

3 回答 3

1

对于第 2 部分,您可以尝试

    Select S.Sname,P.pname,c.cost  from Suppliers As S, Parts AS P, Catalog AS C
    where S.sid=C.Sid And P.pid     =C.pid     
    And color    in ('green','red')
        And c.cost in 
            (Select max(cost) from Catalog AS C1,Parts AS P1 where c1.pid=P1.pid and P1.color=P.color)
于 2013-05-09T06:18:40.263 回答
1

第1部分

从目录中选择 pid,其中 cost<200 按 pid 分组,count(Sid)>=(SelecT Count(sid) from Suppliers)

Sql 小提琴演示

第2部分-:

  Select t.sname,Max(Catalog.cost) from (SELECT  S.sname,c2.cost,c2.sid
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'
) ) t inner join Catalog on t.sid =Catalog.sid
group by t.sid

Sql 小提琴演示

于 2013-05-09T06:21:54.800 回答
1

对于第 1 部分,我认为此查询更具可读性并返回您的预期结果:

SELECT a.pid
FROM
(
    SELECT pid, MAX(cost) max_price, COUNT(1) amount 
    FROM Catalog 
    GROUP BY pid
) a
WHERE
    a.amount = (SELECT COUNT(1) FROM Suppliers)
    AND max_price < 200

现在与你写的相反,这里是你需要EXISTS检查供应商是否有红色或绿色部分的地方:

SELECT 
    sid
FROM
    Suppliers
WHERE
    EXISTS
    (
        SELECT 1
        FROM
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Parts.color = 'green'
    )
    AND EXISTS
    (
        SELECT 1
        FROM
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Parts.color = 'red'
    )

最后,完整的结果应该如下所示:

SELECT 
    sid,
    (
        SELECT pname
        FROM 
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Catalog.cost = (SELECT MAX(c.cost) FROM Catalog c WHERE c.sid = Suppliers.sid)
        LIMIT 1
    ) name,
    (
        SELECT cost
        FROM 
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Catalog.cost = (SELECT MAX(c.cost) FROM Catalog c WHERE c.sid = Suppliers.sid)
        LIMIT 1
    ) cost
FROM
    Suppliers
WHERE
    EXISTS
    (
        SELECT 1
        FROM
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Parts.color = 'green'
    )
    AND EXISTS
    (
        SELECT 1
        FROM
            Catalog
            LEFT JOIN Parts ON Catalog.pid = Parts.pid
        WHERE
            Catalog.sid = Suppliers.sid
            AND Parts.color = 'red'
    )

SQL小提琴在这里

于 2013-05-09T06:01:31.487 回答