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的結果下再去除重復數據