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的结果下再去除重复数据

posted @ 2020-10-31 09:53  大熊童鞋  阅读(2315)  评论(0编辑  收藏  举报