MySQL Case when user example

Source: Internet
Author: User

See some friends in the use of BIND-DLZ when the MySQL statement smattering, simply to write a few examples of their own experiment, deepen the impression, nonsense said, straight solution on the material:

1) Original record

Mysql> select Id,name from test;
+------+------+
| id   | name |
+------+------+
|    1 | lili |
|   | yaya |
|   | cici |
+------+------+

2) syntax
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) Example
--------------  Example-----------------------
3.1 Example 1

Mysql> select Name,
Case
-id< ' 9 ' Then ' Yong '
-When id> ' and ' old '
-Else ' OK ' END Yorn
From Test;
+------+------+
| name | Yorn |
+------+------+
| Lili | Yong |
| Yaya | Ok |
| CiCi | Old |
+------+------+
3.2 Example 2

#当end后面没有用于该列的简写时
Mysql> select Name,
Case
-id< ' 9 ' Then ' Yong '
-When id> ' and ' old '
-Else ' OK ' END
From Test;

+------+------------------------------------------------------------------------------------------------------- --------------+
| name | Case
When id< ' 9 ' Then ' Yong '
When id> ' and ' old '
Else ' OK ' END |
+------+------------------------------------------------------------------------------------------------------- --------------+
| Lili | Yong |
| Yaya | Ok |
| CiCi | Old |
+------+------------------------------------------------------------------------------------------------------- --------------+

3.3 Example 2

#任意列都可以为when的值, such as ID
Mysql> select Name,
Case
When id< ' 9 ' Then ' Yong '
When id> ' "then ID
Else ' OK ' END yarn
From Test;

+------+------+
| name | Yarn |
+------+------+
| Lili | Yong |
| Yaya | 10 |
| CiCi | Old |
+------+------+





This article is from the blog blog, please be sure to keep this source http://cwind.blog.51cto.com/62582/1605209

MySQL Case when user example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.