Use CASE in MySQL and use MySQLCASE

Source: Internet
Author: User

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.

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.