mysql 中 case when then .... else end 的簡單使用

來源:互聯網
上載者:User

標籤:http   學生   span   mysq   mysql   bubuko   學校   color   name   

資料SQL CASE 運算式是一種通用的條件運算式,類似於其它語言中的 if/else 語句。 

CASE WHEN condition THEN result 

   WHEN condition THEN result 

   .............
   [WHEN ...] 
   [ELSE result] 
END 

CASE 子句可以用於任何錶達式可以有效存在的地方。 condition 是一個返回boolean 的運算式。 如果結果為真,那麼 CASE 運算式的結果就是合格 result。 如果結果為假,那麼以相同方式搜尋任何隨後的 WHEN 子句。 如果沒有 WHEN condition 為真,那麼 case 運算式的結果就是在 ELSE 子句裡的值。 如果省略了 ELSE 子句而且沒有匹配的條件, 結果為 NULL。

或其文法為:

簡單Case函數 
CASE sex 
         WHEN ‘1‘ THEN ‘男‘ 
         WHEN ‘2‘ THEN ‘女‘ 
ELSE ‘其他‘ END 

建議都使用第一種,少記點,也好理解。

例子:如下某學校在2005和2006年比賽的資料,

 

1)將 win_loss 中的勝,負,平 都變成 對應的 ‘win’,‘loss‘,‘tie‘

select date_year,    case     when win_loss=‘勝‘ then ‘win‘    when win_loss=‘負‘ then ‘loss‘     else ‘tie‘     end win_loss    from scores;

2) 假設勝得3分,平得一分,負得0分,統計每一年的得分

select date_year ,sum(case when win_loss=‘勝‘ then 3 when win_loss=‘平‘ then 1 else 0 end ) score from scores group by date_year;

3)統計每一年的 勝場數,平場數 和 負場數

select date_year ,sum(case when win_loss=‘勝‘ then 1 else 0 end ) ‘勝場數‘ ,sum(case when win_loss=‘負‘ then 1 else 0 end) ‘負場數‘,sum(case when win_loss=‘平‘ then 1 else 0 end) ‘平場數‘ from scores group by date_year;

由例一可以發現,使用 case when  then  else then 時 是每一條語句都去執行一遍。

例二:資料集如下:

 

試試看:

select tname, case when ttype = ‘語文‘ then tscor else 0 end from testScore

1)用一行來顯示一個學生的成績

select tname, tscorfrom testScore group by tname;

select tname, (case when ttype=‘語文‘ then tscor else 0 end) ‘語文‘, (case when ttype =‘數學‘ then tscor else 0 end) ‘數學‘, (case when ttype =‘英語‘ then tscor else 0 end) ‘英語‘from testScore group by tname;

select    tname as ‘姓名‘ ,         max(case ttype when ‘語文‘ then tscor else 0 end) ‘語文‘,         max(case ttype when ‘數學‘ then tscor else 0 end) ‘數學‘,         max(case ttype when ‘英語‘ then tscor else 0 end) ‘英語‘     from testScore     group by tname;
select tname, max(case  when ttype=‘語文‘ then tscor else 0 end) ‘語文‘, max(case when ttype =‘數學‘ then tscor else 0 end) ‘數學‘, max(case when ttype =‘英語‘ then tscor else 0 end) ‘英語‘from testScore group by tname;

這兩是是同樣的結果。

對比上面,聚和函數的作用。。。。??

 2)統計學生文科,理科的總分。

select tname as ‘姓名‘,       case        when ttype=‘數學‘ then ‘理科‘ else ‘文科‘ end as ‘科別‘,        sum(tscor) as ‘總分‘    from testScore    group by tname,             case              when ttype=‘數學‘ then ‘理科‘ else ‘文科‘ end ;

 

mysql 中 case when then .... else end 的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.