2

有没有办法从 Mysql 中的某个日期开始按 7 天的间隔(日期时间)对行进行分组?

4

3 回答 3

16
SELECT 
    1 + DATEDIFF(columnDate, @start_date) DIV 7  AS weekNumber
  , @start_date + INTERVAL (DATEDIFF(columnDate, @start_date) DIV 7) WEEK
      AS week_start_date
  , MIN(columnDate) AS actual_first_date
  , MAX(columnDate) AS actual_last_date
  , SUM(otherColumn)
  , AVG(otherColumn)
  --- 
FROM 
    tableX 
WHERE 
    columnDate >= @start_date 
GROUP BY
    DATEDIFF(columnDate, @start_date) DIV 7 ;
于 2013-01-17T13:34:15.553 回答
2
SELECT *
FROM  `table` 
GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) ) 
于 2013-01-17T12:46:14.307 回答
0
SELECT users.* from users 
WHERE created_at >= '2011-12-01' 
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)

这将选择在 201-12-01 和 7 天之后创建的用户。进行更改,根据您的需要进行查询。

于 2013-01-17T12:41:50.973 回答