从特定日期开始按7天的间隔对行进行分组

时间:2013-01-17 12:39:45

标签: php mysql

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

3 个答案:

答案 0 :(得分:9)

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 ;

答案 1 :(得分:2)

SELECT *
FROM  `table` 
GROUP BY WEEK( ADDDATE( `date_column` , WEEKDAY(NOW()) ) ) 

答案 2 :(得分: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天。 进行更改,根据您的需要进行查询。