mysql exists与not exists实例详解
程序员文章站
2022-06-29 14:45:09
mysql exists与not exists实例详解
tablea
|column1 | column1 |column3 |
tableb
|column1 |...
mysql exists与not exists实例详解
tablea
|column1 | column1 |column3 |
tableb
|column1 | column1 |column3 |
要查询 tablea 的数据,条件是是 tablea.column1 不在 tableb 的 tableb.column2 中
也就是要得到类似以下语句的效果(not in 效果不完全等同于 not exists , 如果子查询中出现空记录, 则整个查询语句不会返回数据)
select a.* from tablea a where a.column1 not in ( select column2 from tableb )
可以使用如下语句来实现
select a.* from tablea a where not exists( select b.column2 from tableb b where a.colunm1=b.column2 )
以上只是两张表的情况, 其实在多张表的连接查询中也是比较好用的. 以上写法同样适用于exists
以上就是mysql exists与not exists的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: Python数据类型学习笔记