gpt4 book ai didi

php - 根据mysql cake php中传递的日期范围每周对数据进行分组

转载 作者:搜寻专家 更新时间:2023-10-30 23:48:01 25 4
gpt4 key购买 nike

我有一个包含以下数据的表 jobs

+----+------------+--------+---------------------+
| id | project_id | status | modified |
+----+------------+--------+---------------------+
| 1 | 1 | 0 | 2014-08-23 19:48:48 |
| 2 | 1 | 0 | 2014-08-29 18:18:39 |
| 3 | 1 | 0 | 2014-08-23 19:58:55 |
| 44 | 1 | 0 | 2014-08-23 19:45:50 |
| 45 | 1 | 2 | 2014-08-03 14:38:35 |
| 46 | 1 | 3 | 2014-08-05 14:38:35 |
| 47 | 1 | 2 | 2014-08-07 14:38:35 |
| 48 | 1 | 3 | 2014-08-10 14:38:35 |
| 49 | 2 | 4 | 2014-08-14 14:38:35 |
| 50 | 1 | 0 | 2014-08-23 19:43:01 |
| 51 | 1 | 2 | 2014-08-15 14:38:35 |
| 52 | 1 | 3 | 2014-08-18 14:38:35 |
| 53 | 1 | 0 | 2014-08-23 19:49:05 |
| 54 | 1 | 4 | 2014-08-22 14:38:35 |
| 55 | 1 | 2 | 2014-09-03 14:38:59 |
| 56 | 1 | 0 | 2014-08-23 19:59:23 |
| 57 | 1 | 3 | 2014-08-27 14:38:35 |
| 58 | 1 | 4 | 2014-09-03 14:39:04 |
| 59 | 1 | 0 | 2014-08-27 17:28:45 |
| 60 | 1 | 1 | 2014-08-27 17:29:00 |
| 61 | 1 | 1 | 2014-08-27 17:29:17 |
| 62 | 1 | 0 | 2014-08-29 16:36:07 |
| 63 | 1 | 0 | 2014-08-29 16:37:24 |
| 64 | 1 | 0 | 2014-08-29 18:19:01 |
| 65 | 1 | 0 | 2014-08-29 19:17:20 |
| 66 | 1 | 1 | 2014-08-29 19:17:36 |
| 67 | 1 | 1 | 2014-08-29 19:17:54 |
| 68 | 1 | 0 | 2014-08-29 19:26:16 |
| 69 | 1 | 1 | 2014-08-29 19:26:34 |
| 70 | 1 | 1 | 2014-08-29 19:26:54 |
| 71 | 1 | 0 | 2014-08-30 19:33:59 |
| 72 | 1 | 1 | 2014-08-30 19:34:23 |
| 73 | 1 | 1 | 2014-08-30 19:35:42 |
| 74 | 1 | 1 | 2014-08-30 19:36:39 |
| 75 | 1 | 0 | 2014-08-30 19:40:52 |
| 76 | 1 | 0 | 2014-08-30 20:42:02 |
| 77 | 1 | 0 | 2014-08-30 21:27:38 |
| 78 | 1 | 1 | 2014-08-30 21:28:28 |
| 79 | 1 | 1 | 2014-08-30 21:28:59 |
| 80 | 1 | 0 | 2014-08-30 21:47:02 |
| 81 | 1 | 1 | 2014-08-30 21:47:40 |
| 82 | 1 | 1 | 2014-08-30 21:48:20 |
| 83 | 2 | 3 | 2014-08-23 18:23:12 |
| 84 | 2 | 4 | 2014-08-23 18:23:12 |
| 85 | 1 | 4 | 2014-08-23 18:23:12 |
+----+------------+--------+---------------------+

我想根据从前端传递的“从”和“到”日期每周获取数据。所以让我们假设想要从“2014-07-15”到“2014-09-15”的数据应该每周合并一次,并给出每个项目在特定一周内状态为 (2,3,4) 的所有工作的总和.

我的结果应该是这样的

+--------------+---------------+---------------------+
| project_name | reviewed_jobs | modified |
+--------------+---------------+---------------------+
| Project 1 | 0 | 2014-07-15 14:38:35 |
| Project 1 | 0 | 2014-07-22 14:38:35 |
| Project 1 | 1 | 2014-07-29 14:38:35 |
| Project 1 | 3 | 2014-08-05 14:38:35 |
| Project 1 | 2 | 2014-08-12 14:38:35 |
| Project 1 | 2 | 2014-08-19 18:23:12 |
| Project 1 | 1 | 2014-08-26 19:48:48 |
| Project 1 | 2 | 2014-09-02 18:18:39 |
| Project 1 | 0 | 2014-09-09 14:38:59 |
| Project 2 | 0 | 2014-07-15 14:38:35 |
| Project 2 | 0 | 2014-07-22 14:38:35 |
| Project 2 | 0 | 2014-07-29 14:38:35 |
| Project 2 | 0 | 2014-08-05 14:38:35 |
| Project 2 | 1 | 2014-08-12 14:38:35 |
| Project 2 | 2 | 2014-08-19 18:23:12 |
| Project 2 | 0 | 2014-08-26 19:48:48 |
| Project 2 | 0 | 2014-09-02 18:18:39 |
| Project 2 | 0 | 2014-09-09 14:38:59 |
+--------------+---------------+---------------------+

到目前为止,我使用原始查询和 cake php 方式进行了尝试:

原始查询:

SELECT projects.name as project_name, 
SUM(if((jobs.status=2 || jobs.status=3 || jobs.status=4), 1, 0)) as reviewed_jobs,
jobs.modified FROM jobs LEFT JOIN projects ON jobs.project_id=projects.id
WHERE jobs.modified >= '2014:08:03 00:00:00' AND jobs.modified <= '2014:09:03 23:59:59'
GROUP BY projects.name, WEEK(jobs.modified)
ORDER BY DATE(jobs.modified) ASC;

但它给出了以下结果

+--------------+---------------+---------------------+
| project_name | reviewed_jobs | modified |
+--------------+---------------+---------------------+
| Project 1 | 3 | 2014-08-03 14:38:35 |
| Project 1 | 2 | 2014-08-10 14:38:35 |
| Project 2 | 1 | 2014-08-14 14:38:35 |
| Project 2 | 2 | 2014-08-23 18:23:12 |
| Project 1 | 3 | 2014-08-23 19:48:48 |
| Project 1 | 1 | 2014-08-29 18:18:39 |
| Project 1 | 2 | 2014-09-03 14:38:59 |
+--------------+---------------+---------------------+

这显然是不受欢迎的并且与我想要的不同并且它有一些潜在的问题

  1. 如果它没有获取特定日期(即本例中的 2014-08-17)的数据,它会跳过该周或从可用的下一个日期开始计数。

  2. 结果不是根据查询中传递的日期,而是根据表中的数据(表中可用的日期)。

CakePHP 方式:

$options['joins']=array(array('table' => 'projects',
'alias' => 'Project',
'type' => 'LEFT',
'conditions' => array(
'Project.id = Job.project_id',
)
));

$options['fields'] = array(
'Project.name',
'SUM(if((Job.status=2 || Job.status=3 || Job.status=4), 1, 0)) as reviewed',
'DATE(Job.modified) as modified'
);

$options['order'] = array('modified'=>'ASC');

$options['conditions']=array(
array("Job.modified >=" => $from_date->format("Y-m-d") . ' 00:00:00'),
array("Job.modified <=" => $to_date->format("Y-m-d") . ' 23:59:59')
);

$options['group'] = array('Project.name', 'WEEK(Job.modified)');

任何帮助将不胜感激

谢谢

最佳答案

在 mysql 中,您需要另一个包含所有周日期的表,以便您也可以加入该表并返回工作表中没有条目的周数。

构建周数据的快速而肮脏的方法:

SELECT STR_TO_DATE(CONCAT(yr.yy,'-',wk1.d,wk2.d,'-01'), '%Y-%u-%w') yyyywkd FROM
(SELECT 0 d UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) wk1,
(SELECT 0 d UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) wk2,
(SELECT '2013' yy UNION SELECT '2014') yr
WHERE CONCAT(wk1.d,wk2.d) > 0 AND CONCAT(wk1.d,wk2.d) < 54

在左连接到 jobs 表的子查询中试试这个。

关于php - 根据mysql cake php中传递的日期范围每周对数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25663265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com