学生表名为:student, 单科成绩的字段为:subject。学生名字为:name
查询单科成绩前十:mysql语句为:select * from student order by subject desc limit 10
查询总分成绩前十:mysql语句为:select sum(subject) subject,name from student group by name order by subject desc limit 10
注:
select sum(subject) subject,name
这句的意思是:sum(subject) subject 把单科成绩加总用subject 来命名(就是总成绩),name就是一个字段,这里只有两个字段。
group by name order by subject : group by name 的意思按照名字这一栏来分组,当然,学生成绩表名字有可能是一样的,按照学号是最准确的,这里只是举个例子。
order by subject 这句的意思是按照总分成绩排序,这里的subject 是前面重命名总分的意思。
select sum(subject) as countsubject,name from student group by name order by countsubject desc limit 10
扩展资料:学生成绩表常用sql
1. 在表中插入符合主键
[sql]
/*成绩表*/
CREATE TABLE SC
(
Sid INT REFERENCES Student(Sid), /*学生学号*/
Cid INT REFERENCES Course(Cid), /*课程编号*/
Score INT NOT NULL, /*课程分数*/
PRIMARY KEY(Sid,Cid) /*将学生学号和课程编号设为复合主键*/
)
2. 查询各科成绩最高分,最低分以及平均分
[sql]
SELECT c.Cname, MAX(s.Score) AS Max, MIN(s.Score) AS Min, AVG(s.Score) AS Average
FROM Course c JOIN SC s ON c.Cid = s.Cid
GROUP BY c.Cname
/*此处应注意,若不按照c.Cname进行分组,SQL语句会报错,c.Cname在SELECT语句中不合法,因为它并未出现在聚合函数中也没有出现在GROUP BY语句中*/
3. 查询平均成绩大于80分的学生姓名以及平均成绩
[sql]
SELECT Sname, AVG(Score) AS Average FROM Student JOIN SC
ON Student.Sid=SC.Sid
GROUP BY Sname
HAVING AVG(Score)>80
/*以聚合函数为条件进行删选只能在HAVING语句中进行,WHERE语句不支持聚合函数*/
4. 按总分为学生排名,总分相同名次相同
[sql]
SELECT RANK() OVER (ORDER BY SUM(ss.Score) DESC) AS Rank, s.Sname,
ISNULL(SUM(ss.Score),0)
FROM Student s LEFT JOIN SC ss
ON s.Sid = ss.Sid
GROUP BY s.Sname
ORDER BY SUM(ss.Score) DESC
/*RANK()是SQL Server的一个built-in函数,语法为
RANK() OVER ( [ partition_by_clause ] order_by_clause ).*/
5. 查询总分在100至200之间的学生姓名及总分
[sql]
SELECT s.Sname,SUM(ss.Score) FROM Student s JOIN SC ss ON s.Sid=ss.Sid
GROUP BY s.Sname HAVING SUM(ss.Score) BETWEEN 100 AND 200
select class,total,name from (select *,ywsc+sxsc as total from st ORDER BY total DESC) bwhere
not EXISTS(select * from (select *,ywsc+sxsc as total from st ORDER BY total DESC) c where c.class=b.class and b.total <c.total GROUP BY c.class HAVING COUNT(*)>2 )
ORDER BY b.class,b.total DESC
-------第一步,求总成绩(我把总成绩放在一个视图里了)createviewtgasselect学号,sum(成绩)tfrom成绩表groupby学号-------第二步,将总成绩导入到学生表(先运行上面代码)update学生表set总分=tg.tfrom学生表,tgwhere学生表.学号=tg.学号------第三步,排名次select学号,姓名,总分,isnull((selectsum(1)from学生表where总分>A.总分),0)+1名次from学生表asAorderby(selectsum(1)from学生表where总分>=A.部分)-----------最后,嘿嘿,我能力不足,没做触发器了欢迎分享,转载请注明来源:内存溢出
评论列表(0条)