学生表名为: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
借题主答复另一个问题的答案:
表一(考试名称表):CREATE TABLE `zm_exam` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id,考试表',
`title` varchar(120) NOT NULL DEFAULT '' COMMENT '考试名称',
`t_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '外键,老师表',
`cg_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '外键,班级表',
`exam_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '考试时间',
`status` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT '1正常,2禁用',
`type` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT '考试类别,1单科,2全部',
`remark` varchar(255) NOT NULL DEFAULT '' COMMENT '考试说明',
`createdtime` datetime DEFAULT '0000-00-00 00:00:00',
`changedtime` datetime DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 表二(考试科目记录表):
CREATE TABLE `zm_score` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键id,成绩表',
`e_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '外键,考试表',
`c_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '外键,科目表',
`stu_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '外键,学生表',
`grade` float unsigned NOT NULL DEFAULT '0' COMMENT '成绩分数',
`status` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT '1正常,2禁用',
`remark` varchar(255) NOT NULL DEFAULT '' COMMENT '说明',
`createdtime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`changedtime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8
问题:查出每次考试中,该学生在此次考试中的分数排名:
-- a表、b表同为一模一样的临时表-- 通过比对a、b两表的考试记录id&考试分数个数( WHERE a.id = b.id AND a.grade < b.grade)
SELECT *,
(SELECT COUNT(*)+1 FROM
(SELECT e.`id`,e.`title`,e.`remark`,s.`grade`,s.`stu_id`,e.`exam_time` FROM zm_student stu,zm_exam e,zm_score s
WHERE stu.`id` = 132 AND stu.`status` = 1 AND stu.`c_g_id` = e.`cg_id` AND e.`type` = 1 AND e.`id` = s.`e_id` AND s.`status` = 1 ORDER BY s.`grade` DESC) AS b WHERE a.id = b.id AND a.grade < b.grade) AS paiming -- 名次
FROM
(SELECT e.`id`,e.`title`,e.`remark`,s.`grade`,s.`stu_id`,e.`exam_time` FROM zm_student stu,zm_exam e,zm_score s
WHERE stu.`id` = 132 AND stu.`status` = 1 AND stu.`c_g_id` = e.`cg_id` AND e.`type` = 1 AND e.`id` = s.`e_id` AND s.`status` = 1 ORDER BY s.`grade` DESC) AS a WHERE a.stu_id = 132 ORDER BY id DESC
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)