— 列传行
select name 姓名,
max(case subject when '语文' then score end) 语文,
max(case subject when '数学' then score end) 数学,
max(case subject when '物理' then score end) 物理
from studentA
group by name

— 行转列
select * from (
select 姓名, '语文' as 科目, 语文 as 分数 from studentB
union all
select 姓名, '数学' as 科目, 数学 as 分数 from studentB
union all
select 姓名, '物理' as 科目, 物理 as 分数 from studentB
) a
博主理解:把语文列装换为分数列,本身列头没有了,使用字符来填充列头,代表本身列具备的含义。
单个示例:select ‘语文’ as 科目, 语文 as 分数 from studentB

原文链接:https://www.jianshu.com/p/ecb02658239a
文章评论