exists (sql 返回结果集,为真)
主要看exists括号中的sql语句结果是否有结果,有结果:才会继续执行where条件;没结果:视为where条件不成立。
not exists (sql 不返回结果集,为真)
主要看not exists括号中的sql语句是否有结果,无结果:才会继续执行where条件;有结果:视为where条件不成立。
not exists:经过本人测试,当子查询和主查询有关联条件时,相当于从主查询中去掉子查询的数据。
例如:
test数据:id name
1 张三
2 李四
--无结果
select * from test c where not exists
(select 1 from test t where t.id= '1' )
--返回2 李四
select * from test c where not exists
(select 1 from test t where t.id= '1' and t.id = c.id)