行转列,列转行-转

阅读更多

行转列 -- 用max 找出结果,case
SELECT user_name ,
  MAX(CASE course WHEN '数学' THEN score ELSE 0 END ) 数学,
  MAX(CASE course WHEN '语文' THEN score ELSE 0 END ) 语文,
  MAX(CASE course WHEN '英语' THEN score ELSE 0 END ) 英语
FROM test_tb_grade
GROUP BY USER_NAME;

列转行--一列一列查出,然后将结果 union 成多列
select user_name, '语文' COURSE , CN_SCORE as SCORE from test_tb_grade2
union select user_name, '数学' COURSE, MATH_SCORE as SCORE from test_tb_grade2
union select user_name, '英语' COURSE, EN_SCORE as SCORE from test_tb_grade2
order by user_name,COURSE;

具体例子:http://www.jb51.net/article/109203.htm

你可能感兴趣的:(行转列,列转行)