Mysql group by: sorts the records with the largest time in each record.

Source: Internet
Author: User
Tags mysql tutorial

Mysql tutorial group by, sorting the records with the largest time in each record


Select a. * from test,
(Select aid, max (day) max_day from test group by aid) B
Where a. aid = B. aid and a. day = B. max_day
Order by a. install desc

Test SQL


Create table if not exists 'test '(
'Id' int (10) unsigned not null auto_increment,
'Install' int (10) unsigned not null,
'Day' int (10) unsigned not null,
'Aid 'int (10) unsigned not null,
Primary key ('id ')
) Engine = myisam default charset = utf8 auto_increment = 12;

Insert into 'test' ('id', 'install', 'day', 'aid ') values
(1, 1232,200, 1 ),
(2, 2321,200 80809, 2 ),
(3, 1236,200 80810, 3 ),
(5, 4212,200 80809, 1 ),
(6, 2312,200 80810, 1 ),
(7, 1432,200, 1 ),
(8, 2421,200, 2 ),
(9, 4245,200 80811, 2 ),
(10,565 4, 20080810, 2 ),
(11,412,200, 3 );

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.