查询课程号为“3-245”课程的成绩单,包括position、sno、sname、degree,按position从小到大排序。position为学生该门课程的成绩名次
时间: 2024-03-25 13:35:50 浏览: 65
好的,您的问题是如何查询课程号为“3-245”课程的成绩单,包括position、sno、sname、degree,按position从小到大排序。position为学生该门课程的成绩名次。
以下是查询语句:
```
SELECT R.position, S.sno, S.sname, R.degree
FROM (SELECT T.sno, T.degree, @rownum := @rownum + 1 AS position
FROM (SELECT sno, degree
FROM score
WHERE cno = '3-245'
ORDER BY degree DESC) AS T,
(SELECT @rownum := 0) AS R) AS R,
student AS S
WHERE R.sno = S.sno
ORDER BY R.position;
```
解释如下:
1. 内层查询先查询课程号为“3-245”的成绩,并按成绩从高到低排序。同时使用 MySQL 的用户变量 @rownum 初始化为 0,后面用于计算 position。
2. 外层查询将内层查询中得到的每个学生的成绩按照 position 进行排序,同时查询学生的学号和姓名。
3. 最后将查询结果按照 position 从小到大排序。
希望我的回答能够帮到您!
相关问题
查询课程号为3245 包括position sno sname degree 按照position从小到大排position是学生的成绩排名 永sql语句怎么写
以下是可以实现您要求的 SQL 语句:
```sql
SELECT sc.position, s.sno, s.sname, s.degree
FROM (
SELECT t1.sno, COUNT(t2.score) + 1 AS position
FROM score t1
LEFT JOIN score t2 ON t1.cno = t2.cno AND t1.score < t2.score
WHERE t1.cno = 3245
GROUP BY t1.sno
) sc
JOIN student s ON sc.sno = s.sno
ORDER BY sc.position ASC;
```
这个语句首先使用子查询计算每个学生在课程号为3245的课程中的成绩排名(使用LEFT JOIN语句进行比较,从而得到排名)。然后,它将结果与学生表连接,以获取每个学生的其他信息,并按照成绩排名(即position)从小到大排序。
查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
可以回答这个问题,查询语句如下:
SELECT SC.Cno, SC.Sno, SC.Degree
FROM SC
WHERE SC.Cno = '3-105' AND SC.Degree > (SELECT SC.Degree FROM SC WHERE SC.Cno = '3-245');
阅读全文
相关推荐















