Rows and columns in mysql sql

1. Column switch

select class_id, MAX (CASE kemu when ' language' then score ELSE 0 end) as ' language',
MAX (the CASE kemu the when 'mathematics' then score ELSE 0 end) as ' mathematics',
MAX (the CASE kemu the when 'English' then score ELSE 0 end) as 'English'
the FROM scoreinfo the GROUP BY stuent_id

 

 2. Create a new table based on the results of the query machine

CREATE table pei_new (select class_id, MAX (CASE kemu when ' language' then score ELSE 0 end) as ' language',
MAX (the CASE kemu When 'Mathematics' then score ELSE 0 end) as ' Mathematics',
MAX (the CASE kemu When 'English' then score ELSE 0 end) as ' English'
the FROM scoreinfo the GROUP BY stuent_id)

 

 3. Copy a table, just copy the table structure

CREATE table pei_new like scoreinfo;

4. The column line switch

 

 

 

SELECT class_id, 'language' as subject, language as achievements FROM pei_new
UNION
SELECT class_id, 'mathematics', mathematics FROM pei_new
of Union
SELECT class_id, 'English', English FROM pei_new

Guess you like

Origin www.cnblogs.com/xianz666/p/12425262.html