我无法按日期(特别是按日期)对获得的数据进行分组。
是否可以在sql中或使用pdo fetchall()执行此操作?
我知道如何在php中以“蹩脚”的方式来实现这一点(循环遍历数据并以我需要的方式处理它以获得所需的结构),但是有没有一种聪明的方法来实现这一点呢?
$sql = "SELECT times.userid AS 'id', times.time AS 'time'
FROM times
WHERE ((times.userid) IN ($ids)) AND (times.time BETWEEN #$from# AND #$to#)
ORDER BY times.time ASC;";
$stmt = $db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP);
return $data;
上述代码的结果是:
[815] => Array
(
[0] => 2018-01-01 05:00:00
[1] => 2018-01-02 08:00:00
[2] => 2018-01-03 03:00:00
[3] => 2018-01-03 06:00:00
[4] => 2018-01-04 03:00:00
[5] => 2018-01-04 04:00:00
[6] => 2018-01-05 01:00:00
[7] => 2018-01-05 03:00:00
[8] => 2018-01-08 04:00:00
[9] => 2018-01-09 05:00:00
)
我想要得到的结果是这样的:
[815] => Array
(
[2018-01-01] => Array
(
[0] => 2018-01-01 05:00:00
)
[2018-01-02] => Array
(
[0] => 2018-01-02 08:00:00
)
[2018-01-03] => Array
(
[0] => 2018-01-03 03:00:00
[0] => 2018-01-03 06:00:00
)
[2018-01-04] => Array
(
[0] => 2018-01-04 03:00:00
[0] => 2018-01-04 04:00:00
)
[2018-01-05 => Array
(
[0] => 2018-01-05 01:00:00
[0] => 2018-01-05 03:00:00
)
[2018-01-08] => Array
(
[0] => 2018-01-08 04:00:00
)
[2018-01-09] => Array
(
[0] => 2018-01-09 05:00:00
)
)
暂无答案!
目前还没有任何答案,快来回答吧!