1.创建测试表及数据sql如下
create table ScoresTable(
Name varchar(50),
ChineseScore int,
MathScore int
)
insert into ScoresTable values('小张',90,95)
insert into ScoresTable values('小王',98,99)
2.表中查询结果如下

3.现需列转行显示,每行显示 姓名,科目,成绩
实现sql如下:
select * from
(
select Name, Subject ='语文',Scores=ChineseScore from ScoresTable
union all
select Name, Subject ='数学',Scores=MathScore from ScoresTable
) t
order by Name ,Subject
运行结果如下,实现了列转行
