0

我正在使用 MySQL。这是我的架构:

供应商(sid:整数,sname:字符串,地址字符串)

部分(pid:整数,pname:字符串,颜色:字符串)

目录(sid:整数,pid:整数,成本:实数)

(主键加粗)

我正在尝试编写一个查询来选择sid提供相同部分的 s 对:

-- Find pairs of SIDs that both supply the same part
SELECT s1.sid, s2.sid
FROM Suppliers AS s1, Suppliers AS s2
JOIN Catalog ON s1.sid = Catalog.sid OR s2.sid = Catalog.sid;

MySQL给了我这个错误:

错误 1054 (42S22):“on 子句”中的未知列“s1.sid”

我究竟做错了什么?

4

6 回答 6

1

您正在混合使用 ANSI-89 和 ANSI-92 JOIN 语法 - 您只能使用其中一种或另一种。ANSI-92:

   SELECT s1.sid, s2.sid
     FROM CATALOG c
LEFT JOIN SUPPLIERS s1 ON s1.sid = c.sid
LEFT JOIN SUPPLIERS s2 ON s2.sid = c.sid

LEFT如果您想查看关联两个供应商的类别,请省略关键字。

ANSI-89 语法在 FROM 子句中声明了所有涉及的表,连接在 WHERE 子句中。

使用 ANSI-92 -有关详细信息,请参阅此问题

于 2010-02-23T22:55:42.853 回答
1

您正在加入 s2 和 Catalog。s1 在该子句中不存在。

于 2010-02-23T22:56:17.907 回答
1

我不明白错误消息,但是:

在这种情况下,我会避免使用 join 。试试这个

SELECT s1.sid, s2.sid
FROM suppliers s1,
     suppliers s2,
     catalog   c1,
     catalog   c2
WHERE c1.pid = c2.pid
AND   s1.sid = c1.sid
AND   s2.sid = c2.sid
AND   s1.sid < s2.sid

虽然由于您所要求的只是 sids,但它可以更简单:

SELECT c1.sid, c2.sid
FROM catalog   c1,
     catalog   c2
WHERE c1.pid = c2.pid
AND   c1.sid < c2.sid
于 2010-02-23T23:01:33.653 回答
0

如果您将使用显式连接,我认为您需要显式连接所有表。

例如

-- Find pairs of SIDs that both supply the same part
SELECT 
  s1.sid, 
  s2.sid
FROM 
  Catalog 

    LEFT OUTER JOIN 
  Suppliers AS s1,
    ON Catalog.sid = s1.sid

    LEFT OUTER JOIN
  Suppliers AS s2
    ON Catalog.sid = s2.sid 
于 2010-02-23T22:58:37.183 回答
0

您需要自己加入目录

SELECT 
    pid, 
    c1.sid, 
    c2.sid
FROM Catalog c1
JOIN Catalog c2 ON c1.pid = c2.pid AND c1.sid < c2.sid

< 条件是避免配对(A 提供与 B 相同的 X,因此 B 提供与 A 相同的 X)

于 2010-02-23T23:04:47.057 回答
0

查找有两个或多个供应商的零件:

select part_id
from catalog 
group by part_id
having count(part_id) >= 2

查找这些零件的供应商,更具前瞻性,可以显示两个或多个供应商:

select c.part_id, s.supplier_name 
from catalog c
join supplier s
where c.part_id in (
    select part_id
    from catalog 
    group by part_id
    having count(part_id) >= 2)
order by c.part_id, s.supplier_name

但是如果您想要只有两个供应商的零件:

select c.part_id, group_concat(s.supplier_name) as suppliers 
from catalog c
join supplier s using(supplier_id)
where part_id in (
    select part_id
    from catalog 
    group by part_id
    having count(part_id) = 2)
group by c.part_id

如果您只希望这两个供应商在两列中显示..我也在想... :-)

[更新]

我想到了什么:

select c.part_id, c.min(c.supplier_id) as first, c.max(c.supplier_id) as second 
from catalog c
join supplier s
where c.part_id in (
    select part_id
    from catalog 
    group by part_id
    having count(part_id) = 2)
group by c.part_id
order by c.part_id

获取供应商名称:

select x.part_id, a.supplier_name, b.supplier_name from
(
    select c.part_id, c.min(c.supplier_id) as first, c.max(c.supplier_id) as second 
    from catalog c
    join supplier s
    where c.part_id in (
        select part_id
        from catalog 
        group by part_id
        having count(part_id) = 2)
    group by c.part_id
    order by c.part_id
 ) as x
 join supplier a on x.first = a.sid
 join supplier b on x.second = b.sid
于 2010-02-24T00:08:21.500 回答