我该如何获得年龄最大的名字

表信息:

mysql> select * from stu;
+----+------+------+-------+
| id | name | age  | grade |
+----+------+------+-------+
|  1 | a    |   11 |     1 |
|  2 | b    |   12 |     1 |
|  3 | c    |   13 |     1 |
|  4 | d    |   11 |     2 |
|  5 | e    |   12 |     2 |
|  6 | f    |   13 |     2 |
+----+------+------+-------+
6 rows in set (0.00 sec)

mysql> describe stu;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | char(1) | YES  |     | NULL    |                |
| age   | int(11) | YES  |     | NULL    |                |
| grade | int(11) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)


通过这种方式,我可以获得最大的年龄和等级,但是如何获得年龄最大的名字。

mysql> select max(age),grade from stu group by grade;
+----------+-------+
| max(age) | grade |
+----------+-------+
|       13 |     1 |
|       13 |     2 |
+----------+-------+
2 rows in set (0.00 sec)

最佳答案

您可以尝试以下方法:

SELECT A.*
FROM STU A
INNER JOIN (SELECT MAX(AGE) AS MAX_AGE,GRADE FROM STU GROUP BY GRADE ) B ON A.GRADE = B.GRADE AND A.AGE= B.MAX_AGE

关于mysql - 我怎么能知道他的年纪最大的年龄,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43465665/

10-13 23:22