mysql case when使用者樣本

來源:互聯網
上載者:User

標籤:mysq   select case   case when   

看到一些朋友在使用bind-dlz時寫的mysql語句一知半解,索性自己動手寫幾個樣本實驗一下,加深印象,廢話不多說,直解上料:

1)原始記錄

mysql> select id,name from test;
+------+------+
| id   | name |
+------+------+
|    1 | lili |
|   10 | yaya |
|   30 | cici |
+------+------+

2)文法
CASE input_expression
    WHEN when_expression THEN result_expression
        [ ...n ]
    [
        ELSE else_result_expression
    END


CASE   
WHEN Boolean_expression THEN result_expression
        [ ...n ]
    [
        ELSE else_result_expression
    END

3)樣本
--------------  樣本 -----------------------
3.1樣本1

 mysql>  select name,  
    ->      case   
    ->             when id<‘9‘ then ‘yong‘  
    ->             when id>‘15‘ then ‘old‘  
    ->             else ‘ok‘ END YORN  
    ->     from test;
+------+------+
| name | YORN |
+------+------+
| lili | yong |
| yaya | ok   |
| cici | old  |
+------+------+
3.2樣本2

#當end後面沒有用於該列的簡寫時
mysql> select name,  
    ->        case   
    ->                when id<‘9‘ then ‘yong‘
    ->                when id>‘15‘ then ‘old‘
    ->                else ‘ok‘ END   
    ->        from test;

+------+---------------------------------------------------------------------------------------------------------------------+
| name | case   
               when id<‘9‘ then ‘yong‘
               when id>‘15‘ then ‘old‘
               else ‘ok‘ END |
+------+---------------------------------------------------------------------------------------------------------------------+
| lili | yong                                                                                                                |
| yaya | ok                                                                                                                  |
| cici | old                                                                                                                 |
+------+---------------------------------------------------------------------------------------------------------------------+

3.3樣本2

#任意列都可以為when的值,如id
mysql> select name,  
       case   
               when id<‘9‘ then ‘yong‘
               when id>‘15‘ then id
               else ‘ok‘ END yarn  
       from test;

+------+------+
| name | yarn |
+------+------+
| lili | yong |
| yaya | 10   |
| cici | old  |
+------+------+





本文出自 “風雲的部落格” 部落格,請務必保留此出處http://cwind.blog.51cto.com/62582/1605209

mysql case when使用者樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.