zoukankan      html  css  js  c++  java
  • mysql case的语法

    测试表:team

    第一种语法:

    CASE case_value
        WHEN when_value THEN statement_list
        [WHEN when_value THEN statement_list] ...
        [ELSE statement_list]
    END CASE
    mysql> select * from team;
    +------+
    | name |
    +------+
    | a    |
    | b    |
    | c    |
    | d    |
    +------+
    4 rows in set (0.00 sec)
    
    mysql> select case name when 'a' then 'aaa' when 'b' then 'bbb'  when 'c' then 'ccc' when 'd' then 'ddd' end from team;
    +------------------------------------------------------------------------------------------------+
    | case name when 'a' then 'aaa' when 'b' then 'bbb'  when 'c' then 'ccc' when 'd' then 'ddd' end |
    +------------------------------------------------------------------------------------------------+
    | aaa                                                                                            |
    | bbb                                                                                            |
    | ccc                                                                                            |
    | ddd                                                                                            |
    +------------------------------------------------------------------------------------------------+
    4 rows in set (0.00 sec)
    
    mysql> select case name when 'a' then 'aaa' when 'b' then 'bbb'  when 'c' then 'ccc' when 'd' then 'ddd' else 'eee' end from team; 
    +-----------------------------------------------------------------------------------------------------------+
    | case name when 'a' then 'aaa' when 'b' then 'bbb'  when 'c' then 'ccc' when 'd' then 'ddd' else 'eee' end |
    +-----------------------------------------------------------------------------------------------------------+
    | aaa                                                                                                       |
    | bbb                                                                                                       |
    | ccc                                                                                                       |
    | ddd                                                                                                       |
    +-----------------------------------------------------------------------------------------------------------+
    4 rows in set (0.00 sec)

    第二种语法:

    CASE
        WHEN search_condition THEN statement_list
        [WHEN search_condition THEN statement_list] ...
        [ELSE statement_list]
    END CASE
    mysql> select (case when name='a' then 'aaa' when name='b' then 'bbb' when name='c' then 'ccc' when name='d' then 'ddd' else 'ccc' end) alias from team;
    +-------+
    | alias |
    +-------+
    | aaa   |
    | bbb   |
    | ccc   |
    | ddd   |
    +-------+
    4 rows in set (0.00 sec)
  • 相关阅读:
    javascript基础学习三---DOM操作
    小程序-微信开发者工具使用
    回溯算法实践--工作分配问题
    回溯算法理解
    贪心算法--删数问题
    单线程与多线程的区别
    【图解】Web前端实现类似Excel的电子表格
    详细了解JS Map,它和传统对象有什么区别?
    带你彻底弄懂nth-of-type与nth-child的区别
    input 纯数字输入 限制长度 限制 最大值
  • 原文地址:https://www.cnblogs.com/cqdxwjd/p/9919123.html
Copyright © 2011-2022 走看看