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

标签 php mysql

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

最佳答案

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 ;

关于php - 从特定日期开始按 7 天间隔对行进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14379337/

相关文章:

java - 在 Spring 环境中泄漏 Tomcat JDBC MySQL 驱动程序

php - 从 mysql 输出数据到数组 json 让我抓狂

php - Ajax不调用回调函数

php - 我想显示表的值而不是在 laravel 中显示 id

php - 从 mysli_fetch_array 获取选定值并输出

php - 使用钩子(Hook)在 codeigniter 中查询日志

MySQL:使用多个连接获取非空值

php - MySQL - 转换和比较查询中的日期

php - 同域多表单认证

javascript - Ajax 成功中 Laravel Route 的多个参数