Summarizes the case when using

https://blog.csdn.net/changxiangyangy/article/details/86718551

 

A few days ago, in order to analyze the current product user data structure, used when writing sql to the case when, to sum up today about the use of the case when, as a warning, please feel badly written Paizhuan, also written by feeling Yes, to a point man like this, or to reply and let me realize I'm not alone in the battle, well ado, into the topic.
About the use case when I conclude there are three, first, equivalent conversion, second, the scope of the conversion, the third column switch operation.
Equivalent Conversion
They are always in the design of the database will store the user's gender with int ( '0' for women, '1' for men), but how to convert it into a character display it?
The original table data
 
SQL statements
the SELECT
name AS 'name',
(Case Sex 0 the when the then 'female' else 'male' end) as 'sex'
from test.student;
ps. The last 'end' do not lose, I just started using, I had this trouble, and the general case when the statement will be relatively long, it is best to add parentheses wrap, it is easier to read.
search result
 
Range conversion
Sometimes, will encounter this situation, display excellent (90+), good (80-90), pass (60-80), it did not pass (60) according to user scores, the first with a different yes, he is a fraction of the range, how to convert man show? You may feel very simple, it is not replaced when that condition it? First he paused to look down
The original table data
 
 
SQL statements
SELECT
name AS 'name'
, (Case Score When Score> = 90 the then 'excellent' when score> = 80 then 'good' when score> = 60 then 'pass' else 'fail' end) as 'Level'
from Test .stu_score;
So do not write?
The result is check out
 
 
 
This is why it? I want to understand it?
Because the case when as a switch case statement, if you fill something in the case, it will take it after comparison with when, after writing case we write = a score and when later wrote a score> = 90, however, , 'score' equal 'score> = 90' it? obviously not equal, then how to write it?
SELECT
name AS 'name'
, (Case Score When Score> = 90 the then 'excellent' when score> = 80 then 'good' when score> = 60 then 'pass' else 'fail' end) as 'Level'
from Test .stu_score;
 
 
Column switch operation
Or students with examples of right now there is 1 student achievement data, now according to Figure 2 shows how out of it?
figure 1
 
figure 2
 
 
The first step is separated by subject, in line with the conditions set points, to zero non-compliance.
select name as 'name'
, (Case Course, the when 'language' then score else 0 end) as ' language'
, (Case Course, the when 'mathematics' then score else 0 end) as ' mathematics'
, (Case Course, the when 'English' then score else 0 end) as 'English'
from test.course_score
 
 
And then follow the name group by, seeking to score max.
select name as 'name'
, max (Case Course When 'language' then score else 0 end) as ' language'
, max (Case Course When 'Mathematics' then score else 0 end) as ' Mathematics'
, max (Case Course When 'English' then score else 0 end) as ' English'
from Group test.course_score by name;
 
 
----------------
Disclaimer: This article is the original article CSDN bloggers "Murong Tian", the follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/changxiangyangy/article/details/86718551

Guess you like

Origin www.cnblogs.com/erdou/p/12084559.html