为什么 MySQL INTERSECTION 语法错误?

Why is this MySQL INTERSECTION with wrong syntax?

我有两个表,ab:

+-------------+--------+-----------+
| ID_do_aluno | Nome   | Sobrenome |
+-------------+--------+-----------+
|   154676754 | John   | Brown     |
|   232565690 | George | Yellow    |
|   345896580 | Anne   | Green     |
|   459986789 | Ted    | Purple    |
+-------------+--------+-----------+
+-------------+--------+-----------+
| ID_do_aluno | Nome   | Sobrenome |
+-------------+--------+-----------+
|   154676754 | John   | Brown     |
|   232565690 | George | Yellow    |
|   342889999 | Rich   | White     |
+-------------+--------+-----------+

我想找到他们的交集。我试过了

(select * a)INTERSECT(select * from b);

和许多其他变体,但无法正确处理。 我需要的是

+-------------+--------+-----------+
| ID_do_aluno | Nome   | Sobrenome |
+-------------+--------+-----------+
|   154676754 | John   | Brown     |
|   232565690 | George | Yellow    |
+-------------+--------+-----------+

在此先致谢,对于初学者难以在论坛和文档中找到这个经过充分讨论的主题,我们深表歉意!

MySQL 不做 INTERSECT ,可悲的是。

MySQL中没有intersect。相反,使用 existsin:

select ID_do_aluno, Nome, Sobrenome
from a
where (a.ID_do_aluno, a.Nome, a.Sobrenome) in (select b.ID_do_aluno, b.Nome, b.Sobrenome from b)

模拟 MySql 不支持的 INTERSECT 的正确方法是将 a 的每一列与其对应的 b 列与空值进行比较安全相等运算符 <=>,因此 null 值将被视为相等。
此外,INTERSECT,在支持它的数据库中,returns 个不同的行。

SELECT DISTINCT a.ID_do_aluno, a.Nome, a.Sobrenome
FROM a
WHERE EXISTS (
  SELECT 1 FROM b
  WHERE b.ID_do_aluno <=> a.ID_do_aluno AND b.Nome <=> a.Nome AND b.Sobrenome <=> a.Sobrenome
)