简体   繁体   中英

Get row with max value in one column for MySQL query with grouped resultset

I have a mysql table that looks like this (excerpt):

| gameID | gameDATE            |
+--------+---------------------+
| 12757  | 2015-08-15 13:30:00 | 
| 12758  | 2015-08-16 11:00:00 |
| 12759  | 2015-08-16 13:00:00 |
| 12760  | 2015-08-16 13:00:00 |
| 12761  | 2015-08-16 13:00:00 |
| 12762  | 2015-08-16 13:00:00 |
| 12763  | 2015-08-16 13:00:00 |
| 12764  | 2015-08-16 13:00:00 |
| 13335  | 2015-08-16 13:00:00 | 

Now I would like to find the gameDATE which most of the games are played on. I can't seem to find the right query for it.
This is where I always end up with..

SELECT count(gameID) as c, `gameDATE`    
FROM `SKGV2_games`  
WHERE gameROUND =2 AND leagueID =185 GROUP BY `gameDATE` ASC 

I just want one row as a result => the one with the highest number in for c , not all rows.

| c | gameDATE             | 
+---+----------------------+
| 1 | 2015-08-15 13:30:00  |
| 1 | 2015-08-16 11:00:00  |
| 7 | 2015-08-16 13:00:00  |

Try this:

SELECT count(gameID) as c, `gameDATE`    
FROM `SKGV2_games`  
WHERE 
   gameROUND =2 AND leagueID =185 
GROUP BY `gameDATE` 
ORDER BY c ASC
LIMIT 1

UPDATE

This is a suggestion on how to organize your data. As you will have a lot of data in a table like yours it is better to have a parallel table with games and counts per days, and with each entry in this log table you augment the count on the second table. It is more efficient later when you want to build statistics and such.

@DanielSetréus lead me to the 'right' query. The new query is:

SELECT  `gameDATE` 
FROM  `SKGV2_games` 
WHERE gameROUND =2
AND leagueID =185
GROUP BY  `gameDATE` 
ORDER BY COUNT( gameID ) DESC 
LIMIT 1

which just gives me the date (i actually don't need the count of items with the same date).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM