mysql中case when then end的含義和用法


參見這篇博客:https://blog.csdn.net/rongtaoup/article/details/82183743
case when then else end 語句用於查詢滿足多種條件的情況,類似java中的if...else,還有的就是用於進行行轉列的查詢,這個是放在select 子句后面的,充當的是字段的作用。具體用法分為兩種,一種是簡單的函數形式,另一種就是表達式的形式。
簡單的函數形式:case 字段 when 值 then 結果 else 其他情況 end;

表達式的形式:case when 字段=值(這里寫表達式,例如 score=80) then 結果 else 其他情況 end;

簡單的函數形式
select (case score when 'a' then '優秀' else '良好' end) as score from student;

表達式形式
select (case when score between 85 and 90  then '優秀' else '良好' end) as score from student;

在case里面還可以嵌套子查詢等復雜用法。

(case when then end)就是select后面需要查詢出來的一個字段,在使用時可以用小括號括起來可讀性更高,對其可以使用聚合函數,別名,去重,等操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM