Grouping MySQL query by date

Working on a statistics plugin I needed a way to group records by the day.  Turns out it's remarkably easy.

SQL:
  1. SELECT COUNT(id), theDate FROM TABLE GROUP BY YEAR(theDate), MONTH(theDate), DAY(theDate);

If you just want to group by year and month take off the DAY() part.  This would also be a good query to use WEEK() on to pull records grouped by the week they were posted.

Bookmark and Share

Post Info

Leave a Reply