SQL:帮助进行嵌套查询
这是我的架构:
供应商(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
Use:
不知道它如何转换为 MySQL,但大致如下:
注意,标记子查询“a”是一个任意名称,因为 MSSQL 由于某种原因需要在其子查询上使用名称。不知道它在 MySQL 中是如何工作的。
Not sure how it translates to MySQL, but something along these lines:
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.
尝试(未经测试):
Try (untested):
尝试:
Try: