Group by week mysql

How to group by week on mysql

Group by week in mysql is common specially for reporting. Here is how to do it.

If the column is with format yyyy-mm-dd


SELECT COUNT(*), YEARWEEK(__DATE_COLUMN__)
FROM __TABLE__ 
WHERE (__DATE_COLUMN_) > '2018-03-01 00:00:00'
GROUP BY YEARWEEK(__DATE_COLUMN__)

If the column is unixtime stamp format


SELECT COUNT(*), YEARWEEK(FROM_UNIXTIME(__DATE_COLUMN__))
FROM __TABLE__ 
WHERE FROM_UNIXTIME(__DATE_COLUMN__) > '2018-03-01 00:00:00'
GROUP BY YEARWEEK(FROM_UNIXTIME(__DATE_COLUMN__))

Where the __TABLE__ is he mysql table and __DATE_COLUMN__ is the column you are interested.

Access MySQL from Host being on Vagrant

Setting new Vagrant machine for PHP-Mysql development

error duplicate entry for key when creating new unique key mysql

Using mysql keyword as column in Doctrine Entity – PHP

Connecting to Vagrant [PuPHPet] MySql from host

Points on Oracle MySQL Associate Certification Exam

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*