2020 Oracle、mysql经典例题二(如何把学生成绩竖着排列)

本文介绍了如何使用Oracle和MySQL数据库将学生成绩以竖直方式排列的两种方法,详细阐述了操作过程,两种方法都能成功运行。
摘要由CSDN通过智能技术生成

Oracle、mysql经典例题二--如何把学生成绩竖着排列


本例题是基于 Oracle、mysql经典例题一(如何把表格数据竖着排列)题目升级的,没看过之前的可能会有些疑惑。
题目:3张表
Student表
在这里插入图片描述
Course表
在这里插入图片描述
Score表
在这里插入图片描述
如何把这3张表合成下图形式?
在这里插入图片描述
解决:
第一种:简单暴力的

SELECT 姓名,SUM(语文),SUM(数学),SUM(英语),SUM(历史) FROM(
SELECT studentName AS 姓名,score AS 语文,0 AS 数学,0 AS 英语,0 AS 历史 from Student st,Course co,Score sc where st.studentid=sc.studentid and co.CourseId=sc.CourseId
group by StudentName,score,sc.courseId having sc.CourseId='1'
UNION ALL
SELECT studentName AS 姓名,0 AS 语文,score AS 数学,0 AS 英语,0 AS 历史 from Student st,Course co,Score sc where st.studentid=sc.studentid and co.CourseId=sc.CourseId
group by StudentName,score,sc.courseId having sc.CourseId='2'
UNION ALL
SELECT studentName AS 姓名,0 AS 语文,0 AS 数学,score AS 英语,0 AS 历史 from Student st,Course co,Score sc where st.studentid=sc.studentid and co.CourseId=sc.CourseId
group by StudentName,score,sc.courseId having sc.CourseId='3'
UNION ALL
SELECT studentName AS 姓名,0 AS 语文,0 AS 数学,0 AS 英语,score AS 历史 from Student st,Course co,Score sc where st.studentid=sc.studentid and co.CourseId=sc.CourseId
group by StudentName,score,sc.courseId having sc.CourseId=
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值