SQL多表查询行转列

 create table Student

(Id int,Namevarchar(10))

insert Studentselect1 ,'张三'union allselect2 ,'李四'

create table ScoreType(Idint,ScoreNamevarchar(10))

insert ScoreType--Id(主键) ScoreNameselect1 ,'语文'union allselect2 ,'数学'

createtable ScoreS(Idint,StudentIdint,ScoreTypeIdint,Score int)insert ScoreS--Id(主键) StudentId(Student外键) ScoreTypeId(ScoreType外键) Score

select1 ,1 ,1 ,80

union allselect2 ,1 ,2 ,85

union allselect3 ,2 ,1 ,75

union allselect4 ,2 ,2 ,90

select b.Name,

max(casewhen c.ScoreName='语文'then a.Scoreend) as '语文',

max(casewhen c.ScoreName='数学'then a.Scoreend) as '数学

'from Scores a,Student b,ScoreType c

where a.StudentId=b.idand a.ScoreTypeId=c.id

group by b.Name

/*Name 语文 数学---------- ----------- -----------李四 75 90张三 80 85警告: 聚合或其他 SET 操作消除了空值。(2 行受影响)

比较常见的SQL多表查询行转列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值