首页 > 数据库技术 > 详细

MySQL 子查询——查询最大值

时间:2020-04-28 13:39:22      阅读:133      评论:0      收藏:0      [点我收藏+]

       子查询指将一个查询语句嵌套在另一个查询语句中。子查询可以在 SELECT、UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套。在实际开发时,子查询经常出现在 WHERE 子句中。子查询在 WHERE 中的语法格式如下:
       WHERE <表达式> <操作符> (子查询)
       其中,操作符可以是比较运算符和 IN、NOT IN、EXISTS、NOT EXISTS 等关键字。

要求:查出指定范围的最大值,同时显示与之相关的其它信息
实例:查询全班最高分,查询全级最高分,查询全校最高分,同时显示最高分者的姓名,所在班级以及各科成绩情况。
1、查询全班最高分
mysql> select * from studscoreinfo
-> where total_scores = (select max(total_scores) from studscoreinfo where grade_classes =201);
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 22 | 201 | 5 | 陈炜涛 | 88 | 100 | 96 | 284 | 94.7 | |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
1 row in set (0.00 sec)

2、查询全级最高分
mysql> select * from studscoreinfo
-> where total_scores = (select max(total_scores) from studscoreinfo where grade_classes between 101 and 102);
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 9 | 101 | 9 | 赖嘉欣 | 84 | 98 | 96 | 278 | 92.7 | |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
1 row in set (0.00 sec)

3、查询全校最高分
mysql> select * from studscoreinfo
-> where total_scores = (select max(total_scores) from studscoreinfo where grade_classes between 301 and 302);
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| Id | Grade_Classes | Seat_Numbers | Names | Chinese_Scores | Math_Scores | English_Scores | Total_Scores | Score_Averages | Remarks |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
| 38 | 302 | 2 | 陈资彬 | 89.5 | 98 | 98.5 | 286 | 95.3 | |
+------+---------------+--------------+-----------+----------------+-------------+----------------+--------------+----------------+---------+
1 row in set (0.00 sec)

       第一行*是选所有列进行查询,全显示,也可根据需要指定列名查询,第二行中操作符前面的"="可用in代替,效果一样。

MySQL 子查询——查询最大值

原文:https://www.cnblogs.com/comnet/p/12793515.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!