首页 > 解决方案 > 从两个表中选择,其中值不在 MySQL 中的另一个表中

问题描述

我有两个表 table1 和 table2。我想从 table1 中选择,其中 id 不在 table2 id 中。

table1
id     name
001    Testing
002    Hello
003    World
004    Programmer

table2
id     name
001    Testing
003    World

我的期望

id     name
002    Hello
004    Programmer

我试试这些代码

SELECT * FROM table1 as a where a.id <> (SELECT b.id from table2 as b )

这些有什么解决办法吗?

标签: mysql

解决方案


您可以尝试使用JOIN

SELECT * FROM table1 a left join table2 b on a.id=b.id
where b.id is null

推荐阅读