MySQL group BY, sorting the largest record of time in each record

Source: Internet
Author: User
Tags mysql tutorial

MySQL Tutorial group BY, sort the largest record of time in each record


Select A.* from Test A,
(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

Here is the test table, which tests the SQL


CREATE table if not EXISTS ' test ' (
' ID ' int (a) unsigned NOT NULL auto_increment,
' Install ' int (a) unsigned NOT NULL,
' Day ' int (a) unsigned NOT NULL,
' Aid ' int (a) unsigned NOT NULL,
Primary KEY (' ID ')
) Engine=myisam default Charset=utf8 auto_increment=12;

INSERT INTO ' test ' (' ID ', ' Install ', ' Day ', ' aid ') values
(1, 1232, 20080808, 1),
(2, 2321, 20080809, 2),
(3, 1236, 20080810, 3),
(5, 4212, 20080809, 1),
(6, 2312, 20080810, 1),
(7, 1432, 20080811, 1),
(8, 2421, 20080808, 2),
(9, 4245, 20080811, 2),
(10, 5654, 20080810, 2),
(11, 412, 20080808, 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.