3

MySQL には次の関係があります。

Suppliers(**sid**; sname; city),
Parts(**pid**; pname; color),
Catalog(**sid**; **pid**; cost),

キーは間にあります*。

私が見つけたい:

  1. s1 と s2 がまったく同じ部品を供給するようなサプライヤのペア (s1; s2)。
  2. 他の誰からも供給されていない部品を少なくとも 2 つ供給するサプライヤー。
4

3 に答える 3

0

ご回答ありがとうございます。私自身が次のクエリを作成しましたが、それが正しいかどうかはわかりません。私にお知らせください。ありがとう。

1)

SELECT T1.sid、T2.sid

T1、T2から

WHERE (SELECT T1.sid、C2.pid

   FROM( SELECT c1.pid

       FROM Catalog C1

       GROUP BY c1.sid) AS T1, Catalog C2

  WHERE T1.sid=c2.sid)

=

(SELECT   T2.sid, C4.pid

   FROM( SELECT c3.pid

       FROM Catalog C3

       GROUP BY c3.sid) AS T2, Catalog C4

  WHERE T2.sid=c4.sid)

2)

T1.Sを選択

T1から

WHERE NOT EXISTS(( c1.sid を S、c1.pid を p1、C2.pid を p2 として選択

           FROM catalog AS C1, Catalog as C2

            WHERE c1.sid=c2.sid AND

            C1.pid<>c2.pid) AS T1

        EXCEPT

        ( SELECT c3.sid AS S1, c3.pid AS p3, C4.pid AS p4

           FROM catalog AS C3, Catalog as C4

           WHERE c3.sid=c4.sid AND

            c3.pid<>c4.pid) AS T2)

AND NOT EXISTS((c3.sid を S1、c3.pid を p3、C4.pid を p4 として選択

           FROM catalog AS C3, Catalog as C4

           WHERE c3.sid=c4.sid AND

            c3.pid<>c4.pid) AS T2)

        EXCEPT

        (SELECT c1.sid AS S, c1.pid AS p1, C2.pid AS p2

           FROM catalog AS C1, Catalog as C2

            WHERE c1.sid=c2.sid AND

            C1.pid<>c2.pid) AS T1
于 2012-10-09T17:37:55.770 に答える
0

1. s1 と s2 がまったく同じ部品を供給するようなサプライヤのペア (s1; s2):

select c1.sid,c2.sid
from catalog c1 join catalog c2 on c1.pid = c2.pid and c1.sid < c2.sid 
          and not exists 
          (
             select 1 
             from catalog c3 join catalog c4 
             on c3.sid = c1.id and c4.id = c2.sid 
                               and c3.pid <> c4.pid
          )

2.他の誰からも供給されていない部品を少なくとも 2 つ供給するサプライヤー。

select c1.sid
from catalog c1 join catalog c2 on c1.sid = c2.sid and c1.pid < c2.pid 
          and not exists 
          (
             select 1 
             from catalog  
             where sid <> c1.sid and (pid = c1.pid or pid = c2.pid)
          )
于 2012-10-09T14:43:24.767 に答える
0
SELECT supplier_1, supplier_2
FROM (  SELECT c.sid AS supplier_1, c2.sid AS supplier 2, COUNT(*) AS total, COUNT(c2.sid) AS connected
        FROM Catalog AS c
        LEFT JOIN Catalog AS c2
        ON c.sid != c2.sid AND c.pid = c2.pid
        GROUP BY c.sid, c2.sid) AS h
GROUP BY LEAST(supplier_1, supplier_2), GREATEST(supplier_1, supplier_2)

このようなものがうまくいくと思います。基本的に、同じサプライヤと等しい部品 ID に等しくないことに基づいて aを実行しLEFT JOIN、結合の量が、結合が成功せずにカウントされた行の量と等しいかどうかを確認します。

于 2012-10-09T14:44:43.440 に答える