Considerations for using the Max function with GROUP by in MySQL

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.