SQL:相交语法错误?

发布于 2024-08-21 22:23:46 字数 894 浏览 5 评论 0原文

这是我的查询:

-- 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 技术交流群。

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

发布评论

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

评论(6

睫毛溺水了 2024-08-28 22:23:46

您似乎正在使用的 MySQL 不支持 INTERSECT 语法。你将不得不以另一种方式解决它。

在这种情况下,这是微不足道的 - 我们只需要提供某些零件的“绿色”和“红色”的所有供应商的列表 - 您的查询不会费心去查看零件本身是否相关,因此我们可以很容易地解决它像这样:

SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color IN ('red', 'green')
GROUP BY Suppliers.sid
HAVING COUNT(DISTINCT Parts.color) = 2

就我个人而言,我不认为原始查询是典型的 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:

SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color IN ('red', 'green')
GROUP BY Suppliers.sid
HAVING COUNT(DISTINCT Parts.color) = 2

Personally, I don't believe the original query is a typical INTERSECT problem. Take a look at the JOIN solution offered by Vinko Vrsalovic for a general solution to emulate the INTERSECT (which I would btw prefer even if the RDBMS would in fact offer INTERSECT natively).

红ご颜醉 2024-08-28 22:23:46

没什么,MySQL 没有 INTERSECT 关键字。您可以将其重写为 INNER JOIN:

SELECT DISTINCT sid FROM
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "red") a
INNER JOIN
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "green") b
ON (a.sid = b.sid);

这个查询肯定可以写得更好,但这只是为了表明 intersect 只是带有 select unique 的内部联接,您可以自动将一个查询转换为另一个查询。

Nothing, MySQL doesn't have the INTERSECT keyword. You can rewrite it as an INNER JOIN:

SELECT DISTINCT sid FROM
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "red") a
INNER JOIN
(SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
JOIN Parts ON Parts.pid = Catalog.pid
WHERE Parts.color = "green") b
ON (a.sid = b.sid);

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.

百合的盛世恋 2024-08-28 22:23:46

这应该做你想要的:

SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
INNER JOIN Parts AS parts1 ON parts1.pid = Catalog.pid AND parts1.color = "red"
INNER JOIN Parts AS parts2 ON parts2.pid = Catalog.pid AND parts2.color = "green"

This should do what you want:

SELECT Suppliers.sid
FROM Suppliers
JOIN Catalog ON Catalog.sid = Suppliers.sid
INNER JOIN Parts AS parts1 ON parts1.pid = Catalog.pid AND parts1.color = "red"
INNER JOIN Parts AS parts2 ON parts2.pid = Catalog.pid AND parts2.color = "green"
杀お生予夺 2024-08-28 22:23:46

在 MySQL 中使用 INTERSECT 的另一种解决方案是使用 IN 子句。问题:“查找 2009 年秋季提供的课程的课程 ID 以及
Spring 2010”

//DML sample
(select course_id
from section
where semester = ‘Fall’ and year = ‘2009’)
intersect
(select course_id
from section
where semester = ‘Spring’ and year = ‘2010’);

在 MySQL 中:

select distinct course_id
from section
where semester = 'Fall' and year= 2009 and
course_id in (select course_id
from section
where semester = 'Spring' and year= 2010);

如果您需要有关 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"

//DML sample
(select course_id
from section
where semester = ‘Fall’ and year = ‘2009’)
intersect
(select course_id
from section
where semester = ‘Spring’ and year = ‘2010’);

In MySQL:

select distinct course_id
from section
where semester = 'Fall' and year= 2009 and
course_id in (select course_id
from section
where semester = 'Spring' and year= 2010);

If you need more on IN clause , please search on Google.

夜灵血窟げ 2024-08-28 22:23:46

描述:

在此处输入图像描述

假设我们有两个数据库表 T1 和 T2,并且我们需要两个表中的公共元素,那么我们可以使用 INTERSECT 操作,如上图所示。

MySql 中没有 INTERSECT 运算符。因此,我们可以使用以下两个运算符来实现 INTERSECT 概念:

1. IN 子句

当我们想要一列作为 INTERSECT 运算的结果时,我们可以使用 IN 子句。

2. EXISTS 子句

当我们想要多列作为 INTERSECT 操作的结果时,我们可以使用 EXISTS 子句。

有关更多详细信息,您可以在此处阅读文档。

答案:

在上面提出的问题中,他们想要单列作为结果,所以我们可以使用 IN 子句,如下所示:

SELECT Suppliers.sid FROM Suppliers 
INNER JOIN Catalog ON Catalog.sid = Suppliers.sid 
INNER JOIN Parts ON Parts.pid = Catalog.pid 
WHERE Parts.color = "red"
AND Suppliers.sid IN (
    SELECT Suppliers.sid
    FROM Suppliers
    INNER JOIN Catalog ON Catalog.sid = Suppliers.sid
    INNER JOIN Parts ON Parts.pid = Catalog.pid
    WHERE Parts.color = "green")

Description:

enter image description here

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:

SELECT Suppliers.sid FROM Suppliers 
INNER JOIN Catalog ON Catalog.sid = Suppliers.sid 
INNER JOIN Parts ON Parts.pid = Catalog.pid 
WHERE Parts.color = "red"
AND Suppliers.sid IN (
    SELECT Suppliers.sid
    FROM Suppliers
    INNER JOIN Catalog ON Catalog.sid = Suppliers.sid
    INNER JOIN Parts ON Parts.pid = Catalog.pid
    WHERE Parts.color = "green")
节枝 2024-08-28 22:23:46

我认为 SQL 不支持 INTERESCT

尝试这个

SELECT DISTINCT
s.sid
FROM
 suppliers s,
 catalog c
WHERE
 s.sid = c.sid
 AND c.pid IN (SELECT 
 p1.pid
 FROM
 parts p1,
 parts p2
 WHERE
 p1.color = 'red' AND p2.color = 'green');

I think SQL doesn't support INTERESCT

try this one

SELECT DISTINCT
s.sid
FROM
 suppliers s,
 catalog c
WHERE
 s.sid = c.sid
 AND c.pid IN (SELECT 
 p1.pid
 FROM
 parts p1,
 parts p2
 WHERE
 p1.color = 'red' AND p2.color = 'green');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文