Database rank Sql,group by group query by time maximum value

Source: Internet
Author: User

A similar simple table is given first

DROP TABLE IF EXISTS ' Toutiaoanchor '; CREATE TABLE ' Toutiaoanchor ' (  ' hourid ' int (ten) unsigned not NULL,  ' betintime ' varchar (+) NOT NULL,  ' Anchoruid ' int (one) unsigned not null,  ' ticket ' int (ten) unsigned NOT null default ' 0 ' COMMENT ' sparkle value ',  ' pic ' varchar ( DEFAULT ',  ' Info ' varchar (+) NOT NULL,  ' star ' int (one) not null COMMENT ' star value ',  ' hitnum ' int (one) not NUL L COMMENT ' combo times, accumulate continuously, otherwise re-accumulate ',  ' week ' int (one) not null COMMENT ' weeks ',  ' flag ' int (one-by-one) not null COMMENT ' whether to identify the sort 。 1 is used to sort the ',  PRIMARY KEY (' Hourid ')) Engine=innodb DEFAULT Charset=utf8;


Currently doing leaderboard ranking, need to be ranked according to group by, each ranking is in accordance with the first record of each group of the field appears,

For example, I started using

Select Anchoruid,pic,sum (Star) as Starnum,betintime from  toutiaoanchor  GROUP by Anchoruid  ORDER by Starnum Desc,betintime ASC LIMIT 0,3

This time the betintime are used is the first record of time, but I want to use the latest record of the time


So, totally not up to what I wanted.

After analysis, group by is the first piece of data that appears. So I'm going to give him an upside down.

At this point, you need to use a re-form.


Select Anchoruid,pic,sum (Star) as Starnum,betintime from (SELECT * from Toutiaoanchor ORDER by Betintime DESC) TT GROUP by Anchoruid  ORDER by Starnum desc,betintime ASC LIMIT 0,3

That way, it will solve the problem.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Database rank Sql,group by group query by time maximum value

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.