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