当前位置:网站首页>MySQL row to column, column to row, multiple columns to one row, one row to multiple columns

MySQL row to column, column to row, multiple columns to one row, one row to multiple columns

2022-06-12 22:59:00 Witty flowers

This blog is about the interview I saw before SQL topic Used it to learn After reading this blog , I believe you will be right SQL Grammar has different feelings and understandings .

Version Description

mysql8.0

One 、 Transfer line column

demand

classic case when Realization

select

name,

max(case subject when ' Chinese language and literature ' then resuilt else 0 end) ' Chinese language and literature ',

max(case subject when ' mathematics ' then resuilt else 0 end) ' mathematics ',

max(case subject when ' Physics ' then resuilt else 0 end) ' Physics '

from student group by name

Use if sentence

Similar in general case when

select name,

max(if(subject = ' Chinese language and literature ',resuilt,0)) ' Chinese language and literature '

from student group by name

Dynamic splicing sql sentence , No matter how many rows are transferred

set @sql = null;

select group_concat(distinct concat('max(if(a.subject = ''',a.subject,''', a.resuilt, 0)) as ''',a.subject, '''')) into @sql from student a;

set @sql = concat('select name,', @sql, 'from student a group by a.name' );

prepare stmt from @sql; -- Dynamically generate scripts , Prepare a statement

execute stmt; -- Executing scripts dynamically , Execute the prepared statement

deallocate prepare stmt; -- Release prepared statements

Extended analysis

mysql Why do I have to use sum perhaps max

In case of this problem, we can debug sql, To solve our doubts .

Two 、 Column turned

demand

take

Convert to

select
name,
' Chinese language and literature ' as subject,
chinese as resuilt
from -- Note that the normal here should be from Directly followed by a table Lazy don't want to create a table structure Direct subquery ( Row to column code )
(select
name,
max(case subject when ' Chinese language and literature ' then resuilt else 0 end) 'chinese',
max(case subject when ' mathematics ' then resuilt else 0 end) 'math',
max(case subject when ' Physics ' then resuilt else 0 end) 'physics'
from student group by name
) t

union all

select
name,
' mathematics ' as subject,
math as resuilt
from -- Note that the normal here should be from Directly followed by a table Lazy don't want to create a table structure Direct subquery ( Row to column code )
(select
name,
max(case subject when ' Chinese language and literature ' then resuilt else 0 end) 'chinese',
max(case subject when ' mathematics ' then resuilt else 0 end) 'math',
max(case subject when ' Physics ' then resuilt else 0 end) 'physics'
from student group by name
) t

union all

select
name,
' Physics ' as subject,
physics as resuilt
from -- Note that the normal here should be from Directly followed by a table Lazy don't want to create a table structure Direct subquery ( Row to column code )
(select
name,
max(case subject when ' Chinese language and literature ' then resuilt else 0 end) 'chinese',
max(case subject when ' mathematics ' then resuilt else 0 end) 'math',
max(case subject when ' Physics ' then resuilt else 0 end) 'physics'
from student group by name
) t

 

summary

Transfer line column , Use case…when Query data by situation ,group by and sum/max Screening

Column turned , Use... For each column of data required for query union perhaps union all Union

3、 ... and 、 Multiple columns to one row

demand

take

Turn into


select name,GROUP_CONCAT(`subject`,':',resuilt) ' achievement ' from student group by name

GROUP_CONCAT(expr) This function combines the non empty column values according to the grouping conditions and finally returns . If there is a null value , Then return to empty

Four 、 One row to many columns

demand

take

Turn into

 a surface

select 
name,
marks, 
case 
 when locate(' Chinese language and literature ',marks) > 0 then substring_index(substring_index(marks,' Chinese language and literature :',-1),',',1) else 0 end 
as  Chinese language and literature  ,
case 
 when locate(' mathematics ',marks) > 0 then substring_index(substring_index(marks,' mathematics :',-1),',',1) else 0 end 
as  mathematics  ,
case 
 when locate(' Physics ',marks) > 0 then substring_index(substring_index(marks,' Physics :',-1),',',1) else 0 end 
as  Physics  ,
case 
 when locate(' history ',marks) > 0 then substring_index(substring_index(marks,' history :',-1),',',1) else 0 end 
as  history  
from 
(select
name,
GROUP_CONCAT(subject,':',resuilt) marks
from
student group by name) a

It's just the first step give an example The request is still one step away Use union all Continue processing sql

b surface

select
name,
' Chinese language and literature ' subject,
 Chinese language and literature  score
from 
(a surface ) b

union all

select
name,
' mathematics ' subject,
 mathematics  score
from 
(a surface ) b

union all

select
name,
' Physics ' subject,
 Physics  score
from 
(a surface ) b

union all

select
name,
' history ' subject,
 history  score
from 
(a surface ) b

Locate The main function is to determine whether a string contains another string , Such as

Locate(str,sub) > 0, Express sub The string contains str character string ;

Locate(str,sub) = 0, Express sub String does not contain str character string .

substring_index( Parameters str, Parameters delim, Parameters count)

str : What to deal with character string

delim: Separator

count: Count

in other words , If count Positive number , So it's counting from left to right , The first N All of the contents to the left of the separator ! contrary , If it's a negative number , So it's counting from the right , The first N Everything to the right of the separator ,

Reference article

mysql Transfer line column , Column turned , And why it is used when transferring rows to columns max function _ The blog of a daffodil swaying in the wind -CSDN Blog _max function Transfer line column

elementary analysis MySQL in concat as well as group_concat Use - Programmer base

MySQL function Locate Use _ Blog without wind or rain tonight -CSDN Blog _mysql Of locate

MySQL Medium substring_index() Function usage methods and skills _ Geek Xiaojun's blog -CSDN Blog _mysql substring_index

原网站

版权声明
本文为[Witty flowers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122249006110.html