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