MySQL in case use

Source: Internet
Author: User
Tags case statement mysql in

Foreword: Meet such a demand, have a result, the result only records the student's score, now needs to statistic student's grade, 100-90 divides into excellent, 90-60 divides into passes, 60-0 divides into the failure. This requirement can be expressed in case statements.

Case statements are divided into simple case statements and searchable case statements.

1. The syntax for a simple case is

Case  Case_expression while   when_expression_1 then commands when when_expression_2 then   commands   ...   ELSE Commandsend

This is suitable for fixed values such as gender, 1 male, 2 female

Example: The gender of a Chinese character, 1 male, 2 female.

select*, (case sexwhen 1 Then ' Male ' when 2 Then ' female ' END) ' sex ' from ' user '

Results such as

2. The case statement can be searched, and the adaptive expression matches a different set of values.

Syntax for

When the case is    condition_1 then commands the    condition_2 then commands    ...    ELSE Commandsend

The example is the first mentioned

select*, (case is  score>=90 then ' excellent ' when score>=60 Then ' Pass ' ELSE ' failed ' END ') ' Rank ' Frommathorder by score DESC

The results are as follows

MySQL in case use

Related Article

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.