Usage of case when then else end statement

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

--Simple case function

            case sex

            when '1' then '男'

            when '2' then '女'

            else 'other' end

 --case search function--this is often used

            case when sex = '1' then '男'

            when sex = '2' then '女'

            else 'other' end

These two methods can achieve the same function. The writing method of the simple case function is relatively simple, but compared with the case search function, there are some restrictions in terms of functions, such as writing judgment expressions. 
There is another problem that needs to be paid attention to. The case function only returns the first value that meets the conditions, and the remaining case parts will be automatically ignored.

--For example, in the following sql, you will never get the result of "the second type"

            case when col_1 in ('a', 'b') then'first category'

            when col_1 in ('a') then 'second class'

            else 'other' end

Here's the original table:

 Example 1: Look at the difference between select function plus case function and no case function

Without case function, SELECT id, name, core, sex FROM student a;

 Add case function, SELECT (CASE WHEN a.`name`='Zhang San' THEN a.core ELSE 0 END) AS 'Zhang San' , id,name,core,sex FROM student a ;

 It can be seen that adding the case function is a separate column, that is, adding one more column of data than not adding it, and it will not change the number of rows.

Example 2: Introduce the execution process of the case function.

SELECT (CASE WHEN a.`name`='张三' THEN a.core ELSE 0 END) AS '张三' FROM student a ;

 The above execution process:

The match is the name name, first match the first line, name is equal to Zhang San, so the return core is 50
to match the second line, name is equal to Li Si, no match, the return value of else is 0
to match the third line, name is equal to Wang Wu, does not match, the returned value of else is 0
to match the fourth line, name is equal to Zhao Liu, does not match, the returned value of else is 0,
so the result is obtained:

Example 3: Look at the difference between adding or not adding as after the case function

Let's look at the next step, SELECT (CASE WHEN a.`name`='Zhang San' THEN a.core ELSE 0 END) AS 'Zhang San' FROM student a ; 

    

 Then without adding as, SELECT (CASE WHEN a.`name`='Zhang San' THEN a.core ELSE 0 END) FROM student a ; 

 

 It can be seen that adding as is to take an alias for the generated column. If not adding as, the default column name is the entire label of case.

Example 4: Multiple conditional matches in a column

SELECT (CASE WHEN a.`name`='张三' THEN a.core WHEN a.`name`='李四' THEN a.core END) AS '张三' FROM student a ;

 Execution process: the first line matches Zhang San successfully and returns 50, the second line matches Li Si successfully returns 60, and the third and fourth lines do not match and returns null

Example 5: Single condition matching of multiple columns

SELECT (CASE WHEN a.`name`='Zhang San' THEN a.core END) AS 'Zhang San',(CASE WHEN a.`name`='Li Si' THEN a.core END) AS 'Li Si' FROM student a;

Summarize the case statement to convert rows into columns

Example 6: Actual Combat 1

There are the following data:

According to the population data of this country, the population of Asia and North America is counted. Get this result:

 Query sql:

select sum(population),

            case country

            when 'China' then 'Asia'

            when 'India' then 'Asia'

            when 'Japan' then 'Asia'

            when 'United States' then 'North America'

            when 'Canada' then 'North America'

            when 'Mexico' then 'North America'

            else 'other' end

            from   table_a

            group by case country

            when 'China' then 'Asia'

            when 'India' then 'Asia'

            when 'Japan' then 'Asia'

            when 'United States' then 'North America'

            when 'Canada' then 'North America'

            when 'Mexico' then 'North America'

            else 'other' end;

Example 7: Actual Combat 2

There are the following data:

 Grouping by country and gender yields the following results:

Query sql:

select country,

            sum( case when sex = '1' then

            population else 0 end), --male population

            sum( case when sex = '2' then

            population else 0 end) -- female population

            from table_a

            group by country;
 

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/128565940