mysql子查询取最大值_关于【MySQL 子查询——查询最大值】的补充说明
昨天在使用子查询查找最高分和最低分时遇上了一点问题,情况是这样的:如果找到的最高分或最低分是唯一值则不会有什么问题,但如果有其它班级学生的成绩恰好与查询的最高分或最低分相同时就会把那个学生的信息也显示出来,这并不是我们想要的结果。如:mysql> select * from studscoreinfo-> where total_scores in (select max(total_
昨天在使用子查询查找最高分和最低分时遇上了一点问题,情况是这样的:如果找到的最高分或最低分是唯一值则不会有什么问题,但如果有其它班级学生的成绩恰好与查询的最高分或最低分相同时就会把那个学生的信息也显示出来,这并不是我们想要的结果。如:
mysql> select * from studscoreinfo
-> where total_scores in (select max(total_scores) from studscoreinfo where grade_classes = 301);
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 20 | 201 | 3 | 赖晓玲 | 84 | 83 | 88 | 255 | 85 | |
| 31 | 301 | 1 | 陈增光 | 77 | 88 | 90 | 255 | 85 | |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
2 rows in set (0.04 sec)
用之前的方法这里虽然能找到301班最高分的学生,但却把与之同总分另一个学生的信息也显示出来,这并非我们想要的。
又如查询最低分时也遇到了类似的问题:
mysql> select * from studscoreinfo
-> where total_scores in (select min(total_scores) from studscoreinfo where grade_classes = 101);
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 10 | 101 | 10 | 赖丹蕊 | 58 | 88 | 63 | 209 | 69.7 | |
| 65 | 501 | 5 | 吴裕奇 | 65 | 66 | 78 | 209 | 69.7 | |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
2 rows in set (0.00 sec)
这里找到了101班总分最低分的学生,却也把501班一个与她同部分的学生信息也显示出来了。
解决方法很简单,都只是增加一个过滤条件就好。
1、查询最高分:
mysql> select * from studscoreinfo
-> where grade_classes = 301
-> and total_scores in (select max(total_scores) from studscoreinfo where grade_classes = 301);
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 31 | 301 | 1 | 陈增光 | 77 | 88 | 90 | 255 | 85 | |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
1 row in set (0.00 sec)
这里加上了与子查询同过滤条件的语句‘where grade_classes = 301’就可以精准地找了指定的301班总分最高分的学生了。
2、查询最低分:与查询最高分一样,也要增加同过滤条件的语句就好。实例如下:
mysql> select * from studscoreinfo
-> where grade_classes = 101
-> and total_scores in (select min(total_scores) from studscoreinfo where grade_classes = 101);
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 10 | 101 | 10 | 赖丹蕊 | 58 | 88 | 63 | 209 | 69.7 | |
+----+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
1 row in set (0.00 sec)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)