(现在刚学数据库,看到一个题目,自己先做了下,用了三个temp table,很笨拙的方法. 后来才知道这个是 “行转列的问题”,有很简洁的代码,不过不管怎么说,先贴下自己的方法,毕竟是自己辛辛苦苦写的吗,hehe... )
问题:
ID 姓名 科目 分数
01 a 语文 80
01 a 数学 70
01 a 英语 80
02 b 语文 80
02 b 数学 70
02 b 英语 80
怎样用SQL查出
ID 姓名 语文 数学 英语
01 a 80 70 80
02 b 80 70 80
way1 (of myself:)
|
Description | Create three temp tables to store every score apart, then join three table |
| Code |
--Create Test DB --Create Table --Insert sample data --Check the input --Create three temp table Insert Into #ChineseScore Select * from #ChineseScore ---Math Temp Table Insert Into #MathScore Select * from #MathScore ---English Temp Table Insert Into #EnglishScore Select * from #EnglishScore --Get final result |
本文介绍了一种解决数据库中“行转列”问题的方法。通过创建三个临时表分别存储不同科目的成绩,再将这些表进行连接,最终实现了从原始表格到目标表格的数据转换。
2152

被折叠的 条评论
为什么被折叠?



