The difference between mysql case when the column name and the case when the

Recently wrote a sql, only to find that in some cases can not be used when the column name case

(case ts.score when ts.score BETWEEN 9 and 10 then ''  when ts.score BETWEEN 8 and 8.9 then '' when ts.score BETWEEN 7 and 7.9 then '' else ''  END
        )  score_type,

I found out the results of this check writing has been taking the else condition

Baidu a bit later and discovered that the wording is somewhat different in

case has two forms. Simple case function and case search function.

- simple case function
 case Sex 
  the when ' 1 ' the then ' man ' 
  the when ' 2 ' the then ' woman' 
  the else  ' other ' End
 - case search function
 case the when Sex = ' 1 ' the then ' man ' 
     the when Sex = ' 2 ' the then ' woman ' 
     the else  ' other ' End

Both methods can achieve the same functionality. Writing simple case function is relatively simple, but the case and the search function compared to functional aspects will be some restrictions, such as writing style determination.

There is also a need to focus on the problem, only the first case the function returns a value of qualifying, the rest of the case part will be automatically ignored.

Later, I used the search function to write such an approach

CASE 
    WHEN score BETWEEN 9 
    AND 10 THEN
    '' 
    WHEN score BETWEEN 8 
    AND 8.9 THEN
    '' 
    WHEN score BETWEEN 7 
    AND 7.9 THEN
    '' ELSE '' 
END 

 

Guess you like

Origin www.cnblogs.com/NCL--/p/10978280.html