row-to-column, column-to-row-to-turn


Row to column--use max to find the result, case
SELECT user_name ,
  MAX(CASE course WHEN 'math' THEN score ELSE 0 END ) math,
  MAX(CASE course WHEN 'language' THEN score ELSE 0 END ) language,
  MAX(CASE course WHEN 'English' THEN score ELSE 0 END ) English
FROM test_tb_grade
GROUP BY USER_NAME;

column-to-row-check out one column at a time, then union the results into multiple columns
select user_name, 'language' COURSE , CN_SCORE as SCORE from test_tb_grade2
union select user_name , 'Math' COURSE, MATH_SCORE as SCORE from test_tb_grade2
union select user_name, 'English' COURSE, EN_SCORE as SCORE from test_tb_grade2
order by user_name,COURSE;

specific example: http://www.jb51.net/article/109203.htm

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327029511&siteId=291194637