我正在使用MySQL,我有下表:
| clicks | int |
| period | date |
我希望能够生成这样的报告,其中期间在过去4周内完成:
| period | clicks |
| 1/7 - 7/5 | 1000 |
| 25/6 - 31/7 | .... |
| 18/6 - 24/6 | .... |
| 12/6 - 18/6 | .... |
或在过去3个月内:
| period | clicks |
| July | .... |
| June | .... |
| April | .... |
最佳答案
SELECT WEEKOFYEAR(`date`) AS period,SUM(clicks) AS clicks FROM `tablename` WHERE `date` >= CURDATE() - INTERVAL 4 WEEK GROUP BY period SELECT MONTH(`date`) AS period,SUM(clicks) AS clicks FROM `tablename` WHERE `date` >= CURDATE() - INTERVAL 3 MONTH GROUP BY period