mysql语句in的优化


select * from student s where s.stuName in(“张三”,“李四”,“王五”)and s.age>18 and s.sex=‘男’;
优化后的sql:
select * from student s where s.stuName=“张三” and s.age>18 and s.sex=‘男’ 
union all 
select * from student s where s.stuName=“李四” and s.age>18 and s.sex=‘男’
 union all
 select * from student s where s.stuName=“王五” and s.age>18 and s.sex=‘男’ ;

union和union all的用法

将两个select的结果作为一个整体显示出来。

满足条件:

1、两个select查询的列的数量必须相同;

2、每个列的数据类型需要相似;

区别

union all是将两个select语句的结果求并集。 union是将union all的结果下再去除重复数据


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM