Use CASE in MySQL and use MySQLCASE
Syntax description:
Method 1:
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result...] [ELSE result] END
Method 2:
Case when [condition] THEN result [WHEN [condition] THEN result...] [ELSE result] END
Example:
Mysql> select id, name, (gender) as 'gender' from t_user;
+ ---- + ------------ + ------ +
| Id | name | gender |
+ ---- + ------------ + ------ +
| 19 | James | 1 |
| 20 | small red | 2 |
| 21 | super administrator |
+ ---- + ------------ + ------ +
3 rows in set (0.00 sec)
Mysql> select id, name, (CASE gender WHEN 1 THEN 'male' WHEN 2 THEN 'female 'else' other 'ends) as 'gender 'from t_user;
+ ---- + ------------ + ------ +
| Id | name | gender |
+ ---- + ------------ + ------ +
| 19 | Michael Zhang | male |
| 20 | Xiaohong | female |
| 21 | super administrator | others |
+ ---- + ------------ + ------ +
3 rows in set (0.00 sec)
Mysql> select id, name, (case when gender = 1 THEN 'male' WHEN gender = 2 THEN 'female 'else' other 'ends) as 'gene' from t_user;
+ ---- + ------------ + ------ +
| Id | name | gender |
+ ---- + ------------ + ------ +
| 19 | Michael Zhang | male |
| 20 | Xiaohong | female |
| 21 | super administrator | others |
+ ---- + ------------ + ------ +
3 rows in set (0.00 sec)
Application Scenario:
Eg: in the Forum, different types of posts have different types, and the top posts are always set to the top, while the excellent posts are the same as ordinary posts. In this Case, mysql can be used, the type of the excellent post is the same as that of the ordinary post during sorting.
How to Use case when of MySQL
Go to this place and you will see:
Zhidao.baidu.com/question/336637352.html
I have already answered.
Use CASE in MYSQL
I tried it. That's right.