Mysql> SELECT * from test;+----+-------+------+-------+| ID | Name | Age class |+----+-------+------+-------+| Qiu | | 1 | | Liu | | 1 | | Zheng | | 2 | | sec)
If you want to find the largest age in each class, you need to use GROUP by and Max.
If the SQL statement below, the output result is an error:
Mysql> Select Id,name,max (age),Max (sec)
Although the found age is the largest age, the matching user information is not the actual information, but the basic information of the first record after the group by group.
If I use the following statement to find, I can return the actual result.
mysql> select * from (-select * from Test order by age desc) as b group by class;+---- +-------+------+-------+| ID | name | Age | Class |+----+-------+------+-------+| 2 | liu | 1 | | 4 | zheng | | 2 | | 6 | li| 33 | 3 | + ----+-------+------+-------+3 rows in set ( Span class= "Number" >0.00 sec)
Original from: http://blog.csdn.net/magicharvey/article/ Details/21372813?utm_source=tuicool&utm_medium=referral
Considerations for using the Max function with GROUP by in MySQL (go)