mysql - 从两个表中选择,其中值不在 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 )
这些有什么解决办法吗?
解决方案
您可以尝试使用JOIN
SELECT * FROM table1 a left join table2 b on a.id=b.id
where b.id is null
推荐阅读
- express - 路由执行后在中间件中使用更新的 req 变量
- ruby-on-rails - postgres_ext-serializer each_serializer Rails 5 添加自定义 JSON 元数据
- azure-devops - 无法在 azure devops 中使用来自 docker 文件任务的 dep/glide 包来构建来自其他存储库的映像。主机密钥验证失败错误
- datetime - 两个localDate之间的区别
- html5-canvas - 如何在移动设备上的 fabricjs 上启用和处理“点击”事件?
- python - 执行 PCA 后如何绘制每个变量的主向量?
- python - 如何匹配输入数据和df中的数据,for循环减
- jenkins - 在 Jenkins 管道中不起作用凭证
- amp-html - 缺少 AMP HTML 标记
- ios - 我可以将私有变量传递给将通过 Storyboard 实例化的 UIViewController 吗?