Sql 查询学生成绩表中每个科目的最高分及对应科目和学生
一道面試題,現(xiàn)場沒寫正確(默哀),回來記錄一下
學(xué)生成績表?:
CREATE TABLE `tabscore` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `students` varchar(255) DEFAULT NULL,
? `course` varchar(255) DEFAULT NULL,
? `score` float(3,1) DEFAULT NULL,
? PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
INSERT INTO `tabscore` VALUES ('1', '張三', '語文', '75.0');
INSERT INTO `tabscore` VALUES ('2', '張三', '數(shù)學(xué)', '80.0');
INSERT INTO `tabscore` VALUES ('3', '李四', '語文', '60.0');
INSERT INTO `tabscore` VALUES ('4', '李四', '數(shù)學(xué)', '99.0');
INSERT INTO `tabscore` VALUES ('5', '王五', '語文', '85.0');
INSERT INTO `tabscore` VALUES ('6', '王五', '數(shù)學(xué)', '99.0');
?
?
select * from?
(select course, MAX(score) as maxScore from tabscore GROUP BY course) t2?
join ?
tabscore?
on tabscore.course = t2.course and tabscore.score = t2.maxScore?
?
select * from tabscore
where score =
(SELECT MAX(score) as maxscore from tabscore tb2 where tb2.course = tabscore.course)
或者 having
select * from tabscore
where score =
(SELECT MAX(score) as maxscore from tabscore tb2 GROUP BY course having tb2.course = tabscore.course)
3條sql得到的結(jié)果是一致的,至于那個效率高?
?
?
總結(jié)
以上是生活随笔為你收集整理的Sql 查询学生成绩表中每个科目的最高分及对应科目和学生的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android:修改默认音量等级
- 下一篇: 运营商代码全集