在某些条件下找到每件物品和最贵的物品

时间:2013-05-09 05:42:20

标签: mysql sql

我有以下关系和

1.我想找到每个供应商提供的零件以及少于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小时后我会回复。

3 个答案:

答案 0 :(得分: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 fiddle here

答案 1 :(得分: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)

答案 2 :(得分:1)

PART1

从目录中选择pid,其中成本&lt; 200 group by pid having count(Sid)&gt; =(SelecT Count(sid)from Suppliers)

Sql Fiddle Demo

第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 Fiddle Demo