Order by amount of entries in MySQL
I have a table like this id, name, datetime
which might store multiple entries per day, now I am looking for a way to select the day with the highest amount entries in just one query.
Until now I was doing a loop through all the entries and comparing a $highest
integer against every value, but there must an easier way to do it with one sql query. Ideas anyone?
Update:
The table basically looks something like this
id, name, datetime 1, test, 2开发者_开发问答010-12-01 12:12:12 2, test2, 2010-11-30 12:12:12 3, test3, 2010-11-30 13:13:13 4, test4, 2010-10-29 12:12:12
the result would need to be 2010-11-30
because there are two entries from that specific day and it is effectively the "highest day" (or day with the highest amount of entries).
SELECT id, name, DATE(`datetime`) AS `date`, COUNT(*) AS `count`
FROM things
GROUP BY `date`
ORDER BY `count` DESC
If you only want the highest day, add a LIMIT 1
at the end.
Keep in mind that the id
and name
will be from a random record on that day. If you want all of the records on that particular day, you'll need to run another query or use a sub-query.
Try this:
SELECT DATE(`datetime`) AS `date`
FROM your_table
GROUP BY `date`
ORDER BY COUNT(*) DESC
LIMIT 1
精彩评论