[英]SQL Group By Date But Maintain Records
I assume the php/cakephp/sql
parliance. 我假设
php/cakephp/sql
保持原状。 I have a History
table which contains the column created of type DATETIME.
我有一个“
History
表,其中包含创建的DATETIME.
类型的列DATETIME.
I would show all the records of the History but arranged by day. 我将显示所有历史记录,但按天排列。
------------------------------------------
id created what
------------------------------------------
1 2014-01-01 08:00:00 Someone ran
2 2014-01-01 09:00:00 Someone cried
2 2014-01-02 10:00:00 Someone spoke
2 2014-01-02 18:00:00 Someone worked
And the result I am looking for 而我寻找的结果
array(
'2014-01-01' => array(
0=>array('time'=>'8:00','what'=>'Someone ran'
1=>array('time'=>'9:00','what'=>'Someone cried'
),
'2014-01-02' => array(
0=>array('time'=>'10:00','what'=>'Someone spoke',
1=>array('time'=>'18:00','what'=>'Someone worked',
)
)
I am totaly stucked. 我完全被困住了。 Which SQL statement should I investigate?
我应该调查哪个SQL语句? GROUP is not good...
GROUP不好...
If the other reviewers agree, I would give my answer in cakephp adapted from this SO answer 如果其他审稿人同意,我将在cakephp中给出我的答案,改编自该SO答案
$histories = $this->History->find('all',
array(
'group'=>array('History.created')
));
foreach ($histories as $item) {
$key = date('Y-m-d',strtotime($item['History']['created']));
if (!isset($groups[$key])) {
$groups[$key] = array(
'items' => array($item),
'count' => 1,
);
} else {
$groups[$key]['items'][] = $item;
$groups[$key]['count'] += 1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.