今天写查询语句,碰到一个问题,就是两张表里面,a表中有风险等级结果,a表和b表两表之间没有必然关联关系,但是也有一定关系,这个关系很微妙,就是两个表每个人的编号id。
我要查询的是在a表中查询b表业务代码不包含“109”对应人的id,那么需要用到什么关键字
试了下两个形式的查询方法:
1、not in
where T2.id not in(select a.id from a a where a.type='109')
2、not exists
where not exists(select a.id from a a where a.type='109' and T2.id=a.id)
两个查询之后,查询结果一样
试验后,执行语句的书写上稍微有点不同,就是用not exists时,where后面不需要加查询的字段,直接where和not exists相连即可,用not in 需要在where 后面加字段,即查询条件。
感谢:sql中的 in、exist、not in、not exist
https://blog.youkuaiyun.com/tfstone/article/details/81451331
这篇文章