SQLServer列转行操作及union all用法

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

运行结果如下,实现了列转行

相关推荐
Tony Bai2 小时前
“我曾想付钱给 Google 去工作”—— Russ Cox 深度访谈:Go 的诞生、演进与未来
开发语言·后端·golang
Mr.Pascal3 小时前
Redis:主动更新,读时更新,定时任务。三种的优劣势对比
数据库·redis·缓存
思成不止于此3 小时前
【MySQL 零基础入门】DQL 核心语法(二):表条件查询与分组查询篇
android·数据库·笔记·学习·mysql
骥龙4 小时前
3.10、构建网络防线:防火墙、WAF 与蜜罐实战
服务器·网络·数据库·网络安全
serendipity_hky4 小时前
【SpringCloud | 第2篇】OpenFeign远程调用
java·后端·spring·spring cloud·openfeign
嘟嘟MD4 小时前
程序员副业 | 2025年11月复盘
后端·创业
SadSunset4 小时前
(15)抽象工厂模式(了解)
java·笔记·后端·spring·抽象工厂模式
汝生淮南吾在北4 小时前
SpringBoot+Vue养老院管理系统
vue.js·spring boot·后端·毕业设计·毕设
李慕婉学姐4 小时前
【开题答辩过程】以《基于springboot的地铁综合服务管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
期待のcode5 小时前
Springboot配置属性绑定
java·spring boot·后端