提取在另一个表中找不到的记录

时间:2016-04-19 06:12:24

标签: mysql

我有这张桌子......

id          name          location          date_created
--          ----          --------          ------------

15641       Maybel        New York          2015-09-15
84194       Joseph        Arkansas          2015-02-03
36479       Frank         Illinois          2015-10-28
19804       Samantha      San Francisco     2015-11-05
67811       Charles       Texas             2015-11-05

......和这张桌子......

id          name          location          created_at
--          ----          --------          ----------
15641       Maybel        New York          2015-09-15
84194       Joseph        Arkansas          2015-02-03
36479       Frank         Illinois          2015-10-28
78916       Logan         Philadelphia      2015-12-01
26799       George        Mississippi       2015-12-10

我需要的是......

id          name          location          created_at
--          ----          --------          ----------
19804       Samantha      San Francisco     2015-11-05
67811       Charles       Texas             2015-11-05

所以基本上我需要一个查询来只显示表1中未在表2中找到的记录。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用此

SELECT *
FROM table1
WHERE id NOT IN (SELECT id FROM table2)

答案 1 :(得分:0)

SELECT *
FROM table1 t1
Left join table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL;