0

可能重复:
将 SQL 查询写入下表中的 SELECT 项

我正在尝试列出至少有两个不同零件的供应商的供应商名称(sname)和零件编号(pnum)。

这是我需要使用的两张表...

出货量表:http: //i44.tinypic.com/1zdcc9j.jpg

供应商表:http: //i39.tinypic.com/o6w414.png

我尝试了什么:

这是不正确的,我尝试了一些类似的东西......

SELECT snum 
FROM (SELECT snum, count(snum) AS nbr FROM Shipments ) 
WHERE nbr > 1;

谢谢

4

3 回答 3

0

I don't know about Access (as this question is tagged as such), but this should work in MySQL:

select su.sname from suppliers su
join shipments sh on su.snum = sh.snum
group by su.sname
having count(distinct su.pnum) >= 2
于 2012-04-05T02:34:58.110 回答
0
SELECT suppliers.sname, shipments.pnum
FROM suppliers, shipments
WHERE shipments.snum = suppliers.snum
GROUP BY suppliers.sname
HAVING count(shipments.pnum) >= 2
于 2012-04-05T02:30:59.037 回答
0

使用 COUNT 等聚合函数时,您需要使用 GROUP BY 子句。当使用聚合和 GROUP BY 时,HAVING CLAUSE 充当您的 where 子句。

这将为您提供所有计数 > 1 的 snum

SELECT snum
FROM
(
    SELECT snum, count(snum) AS nbr
    FROM Shipiments
    GROUP BY snum
    HAVING COUNT(snum) > 1
) AS T

这会将表连接回供应商表以获取供应商名称

SELECT DISTINCT Suppliers.sname
FROM
(
    SELECT snum, count(snum) AS nbr
    FROM Shipiments
    GROUP BY snum
    HAVING COUNT(snum) > 1
) AS T INNER JOIN Suppliers ON Suppliers.snum = T.snum
于 2012-04-05T02:31:23.047 回答