Karina
Karina

Reputation: 663

MySQL Date Range

I have an automated query that counts the number of searches made each week, at the moment it is showing just the month the search took place in, i would like it to show the date range of the respective week it is showing the data for, i.e 30/09/13 - 07/10/13

My query:

SELECT COUNT(*) AS `count`,
    `region`,
    MONTHNAME(`date`) as `month`
FROM stores.stats
WHERE `date` > DATE_ADD(DATE (NOW()), INTERVAL - 1 WEEK)
    AND `date` < DATE (NOW())
GROUP BY `region`,
    MONTH(`date`);

Upvotes: 0

Views: 112

Answers (1)

Dan
Dan

Reputation: 10700

Just add the range to your select list, like so:

SELECT COUNT(*) AS `count`,
    `region`,
    MONTHNAME(`date`) as `month`,
    DATE_ADD(DATE (NOW()), INTERVAL - 1 WEEK) as `range start`,
    DATE (NOW()) as `range stop`
FROM stores.stats
WHERE `date` > DATE_ADD(DATE (NOW()), INTERVAL - 1 WEEK)
    AND `date` < DATE (NOW())
GROUP BY `region`,
    MONTH(`date`)

Upvotes: 2

Related Questions