SQL:帮助进行嵌套查询

发布于 2024-08-22 12:14:22 字数 777 浏览 7 评论 0原文

这是我的架构:

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

零件(pid:整数, pname:字符串,颜色:字符串)

目录( >sid:整数,pid:整数,cost:实数)

粗体表示主键。

我想编写一个查询来查找提供每个零件的所有供应商。这是我已经有的两个查询:

-- get all parts for a given supplier
SELECT Parts.pid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Suppliers.sid = 4;

-- gets all parts that exist
SELECT Parts.pid
FROM Parts

用命令式的术语来说,我想做的是这样的:

Define result set
Foreach Supplier:
    If the list of parts produced by a supplier 
    is equal to the total list of parts, add this supplier to the result set
Return result set

我怎样才能将其翻译成 MySQL?

Here is my schema:

Suppliers(sid: integer, sname: string, address string)

Parts(pid: integer, pname: string, color: string)

Catalog(sid: integer, pid: integer, cost: real)

bold indicates primary key.

I want to write a query to find all suppliers who supply every part. Here are two queries I have already:

-- get all parts for a given supplier
SELECT Parts.pid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Suppliers.sid = 4;

-- gets all parts that exist
SELECT Parts.pid
FROM Parts

What I want to do, in imperative terms, is something like this:

Define result set
Foreach Supplier:
    If the list of parts produced by a supplier 
    is equal to the total list of parts, add this supplier to the result set
Return result set

How can I translate this into MySQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花桑 2024-08-29 12:14:22

使用:

SELECT s.*
  FROM SUPPLIER s
  JOIN (SELECT c.sid,
               COUNT(c.pid) AS num_parts
          FROM CATALOG c
      GROUP BY c.sid) x ON x.sid = s.sid
  JOIN (SELECT COUNT(*) AS total_parts
          FROM PARTS) y ON y.total_parts = x.num_parts

Use:

SELECT s.*
  FROM SUPPLIER s
  JOIN (SELECT c.sid,
               COUNT(c.pid) AS num_parts
          FROM CATALOG c
      GROUP BY c.sid) x ON x.sid = s.sid
  JOIN (SELECT COUNT(*) AS total_parts
          FROM PARTS) y ON y.total_parts = x.num_parts
划一舟意中人 2024-08-29 12:14:22

不知道它如何转换为 MySQL,但大致如下:

select s.sname, PartCount from (
 select s.SID, s.sname, PartCount = sum(p.PID)
 inner join Catalog c on c.SID = s.SID
 inner join Parts p on p.PID = c.PID
 where s.SID = 4
 group by s.SID
) a
where PartCount = MAX(PartCount)

注意,标记子查询“a”是一个任意名称,因为 MSSQL 由于某种原因需要在其子查询上使用名称。不知道它在 MySQL 中是如何工作的。

Not sure how it translates to MySQL, but something along these lines:

select s.sname, PartCount from (
 select s.SID, s.sname, PartCount = sum(p.PID)
 inner join Catalog c on c.SID = s.SID
 inner join Parts p on p.PID = c.PID
 where s.SID = 4
 group by s.SID
) a
where PartCount = MAX(PartCount)

Note labeling the subquery 'a' is an arbitrary name as MSSQL requires names on its subqueries for some reason. Dunno how it works in MySQL.

巡山小妖精 2024-08-29 12:14:22

尝试(未经测试):

SELECT s.*
FROM (
  SELECT sid, count(pid) as c
  FROM Catalog
  GROUP BY sid) q1
JOIN Suppliers s ON s.sid = q1.sid
WHERE q1.c = (SELECT COUNT(*) FROM Parts)

Try (untested):

SELECT s.*
FROM (
  SELECT sid, count(pid) as c
  FROM Catalog
  GROUP BY sid) q1
JOIN Suppliers s ON s.sid = q1.sid
WHERE q1.c = (SELECT COUNT(*) FROM Parts)
老旧海报 2024-08-29 12:14:22

尝试:

SELECT Suppliers.sid
FROM Suppliers
INNER JOIN
(SELECT sid, COUNT(pid) as num
    FROM Catalog
    GROUP BY sid)as t1
ON Suppliers.sid = t1.sid
WHERE t1.num = 
    (SELECT COUNT(pid) FROM Parts)

Try:

SELECT Suppliers.sid
FROM Suppliers
INNER JOIN
(SELECT sid, COUNT(pid) as num
    FROM Catalog
    GROUP BY sid)as t1
ON Suppliers.sid = t1.sid
WHERE t1.num = 
    (SELECT COUNT(pid) FROM Parts)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文