Mysql 使用Exist替代in查询 提高查询性能
使用 exists 代替 in 是一个好的选择:
select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)
exists语法
用法: exists后面一定是子查询语句,不能用(值1,值2)代替;where exists (查询),结构中没有列;exists后面的子查询不返回任何实际数据,只返回真或假,当返回真时 where条件成立,该条记录保留。
exists (查询),只要子查询不会空 则where条件就返回真。