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