SQL:相交语法错误?
这是我的查询:
-- Sids of suppliers who supply a green part AND a red part
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "red")
INTERSECT
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "green");
这是错误:
错误 1064 (42000):您遇到错误 在你的 SQL 语法中;检查手册 对应于您的 MySQL 服务器 正确使用语法的版本 靠近 “相交 (选择供应商.sid 来自供应商 JOIN Catalog ON Catalog.sid = Sup" 第 6 行。
我做错了什么?
这是架构:
Suppliers(sid: integer, sname: string, address string)
Parts(pid : 整数, pname: 字符串, color: 字符串)
Catalog(sid: 整数, pid: 整数, cost: 实数)
粗体 = 主键
This is my query:
-- Sids of suppliers who supply a green part AND a red part
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "red")
INTERSECT
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "green");
This is the error:
ERROR 1064 (42000): You have an error
in your SQL syntax; check the manual
that corresponds to your MySQL server
version for the right syntax to use
near
"INTERSECT
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Sup" on line 6.
What am I doing wrong?
This is the schema:
Suppliers(sid: integer, sname: string, address string)
Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)
bold = primary key
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您似乎正在使用的 MySQL 不支持 INTERSECT 语法。你将不得不以另一种方式解决它。
在这种情况下,这是微不足道的 - 我们只需要提供某些零件的“绿色”和“红色”的所有供应商的列表 - 您的查询不会费心去查看零件本身是否相关,因此我们可以很容易地解决它像这样:
就我个人而言,我不认为原始查询是典型的 INTERSECT 问题。看一下 Vinko Vrsalovic 提供的
JOIN
解决方案,以获得模拟INTERSECT
的通用解决方案(顺便说一句,即使 RDBMS 实际上会提供INTERSECT
本身)。MySQL, which you appear to be using, does not support the
INTERSECT
syntax. You're going to have to solve it another way.In this case, it is trivial -we only need a list of all suppliers that offer "green" and "red" of some part- your query does not bother to see if the parts themselves are related, so we can solve it quite easily like this:
Personally, I don't believe the original query is a typical
INTERSECT
problem. Take a look at theJOIN
solution offered by Vinko Vrsalovic for a general solution to emulate theINTERSECT
(which I would btw prefer even if the RDBMS would in fact offerINTERSECT
natively).没什么,MySQL 没有 INTERSECT 关键字。您可以将其重写为 INNER JOIN:
这个查询肯定可以写得更好,但这只是为了表明 intersect 只是带有 select unique 的内部联接,您可以自动将一个查询转换为另一个查询。
Nothing, MySQL doesn't have the INTERSECT keyword. You can rewrite it as an INNER JOIN:
This query can surely be better written, but this is to show that intersect is but merely an inner join with a select distinct, you can automatically transform one into the other.
这应该做你想要的:
This should do what you want:
在 MySQL 中使用 INTERSECT 的另一种解决方案是使用 IN 子句。问题:“查找 2009 年秋季提供的课程的课程 ID 以及
Spring 2010”
在 MySQL 中:
如果您需要有关 IN 子句的更多信息,请在 Google 上搜索。
Another solution in order to use INTERSECT in MySQL is to use IN clause. Problem: "Find course id’s of courses offered in Fall 2009 and
Spring 2010"
In MySQL:
If you need more on IN clause , please search on Google.
描述:
假设我们有两个数据库表 T1 和 T2,并且我们需要两个表中的公共元素,那么我们可以使用 INTERSECT 操作,如上图所示。
MySql 中没有 INTERSECT 运算符。因此,我们可以使用以下两个运算符来实现 INTERSECT 概念:
1. IN 子句
当我们想要一列作为 INTERSECT 运算的结果时,我们可以使用 IN 子句。
2. EXISTS 子句
当我们想要多列作为 INTERSECT 操作的结果时,我们可以使用 EXISTS 子句。
有关更多详细信息,您可以在此处阅读文档。
答案:
在上面提出的问题中,他们想要单列作为结果,所以我们可以使用 IN 子句,如下所示:
Description:
Suppose we have two DB tables T1 and T2 and we need common elements from both tables then we can use INTERSECT Operation as shown in the above image.
In MySql there is no INTERSECT operator. So we can implement INTERSECT concept with following two operators:
1. IN Clause
We can use IN clause when we want one column as of result of INTERSECT Operation.
2. EXISTS Clause
We can use EXISTS clause when we want multiple column as of result of INTERSECT Operation.
For more details you can read the documentation here.
Answer:
In above asked question they want single column as of the result, so we can use IN Clause as follows:
我认为 SQL 不支持 INTERESCT
尝试这个
I think SQL doesn't support INTERESCT
try this one