一尘不染

Mysql:从表中选择不在另一个表中的行

mysql

如何选择一个表中所有未出现在另一表中的行?

表格1:

+-----------+----------+------------+
| FirstName | LastName | BirthDate  |
+-----------+----------+------------+
| Tia       | Carrera  | 1975-09-18 |
| Nikki     | Taylor   | 1972-03-04 |
| Yamila    | Diaz     | 1972-03-04 |
+-----------+----------+------------+

表2:

+-----------+----------+------------+
| FirstName | LastName | BirthDate  |
+-----------+----------+------------+
| Tia       | Carrera  | 1975-09-18 |
| Nikki     | Taylor   | 1972-03-04 |
+-----------+----------+------------+

表1中不在表2中的行的示例输出:

+-----------+----------+------------+
| FirstName | LastName | BirthDate  |
+-----------+----------+------------+
| Yamila    | Diaz     | 1972-03-04 |
+-----------+----------+------------+

也许这样的事情应该工作:

SELECT * FROM Table1 WHERE * NOT IN (SELECT * FROM Table2)

阅读 232

收藏
2020-05-17

共1个答案

一尘不染

如果您在另一条注释中提到有300列,并且想要对所有列进行比较(假设这些列的名称相同),则可以使用a NATURAL LEFT JOIN隐式联接两个表之间所有匹配的列名称,以便不必繁琐地手动输入所有加入条件:

SELECT            a.*
FROM              tbl_1 a
NATURAL LEFT JOIN tbl_2 b
WHERE             b.FirstName IS NULL
2020-05-17