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