Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql CakePHP Group By field每个组只返回一个结果_Mysql_Cakephp_Group By_Find - Fatal编程技术网

Mysql CakePHP Group By field每个组只返回一个结果

Mysql CakePHP Group By field每个组只返回一个结果,mysql,cakephp,group-by,find,Mysql,Cakephp,Group By,Find,我有一张表格如下: id | date | customer_id | env_id | jobs 1 | 2012-11-20 | 200 | 100 | 10 2 | 2012-11-20 | 200 | 101 | 15 3 | 2012-11-20 | 200 | 102 | 12 4 | 2012-11-21 | 200 | 100 | 10 5 | 2012-11-21 |

我有一张表格如下:

id | date       | customer_id | env_id | jobs
1  | 2012-11-20 | 200         | 100    | 10
2  | 2012-11-20 | 200         | 101    | 15
3  | 2012-11-20 | 200         | 102    | 12
4  | 2012-11-21 | 200         | 100    | 10
5  | 2012-11-21 | 200         | 101    | 12
6  | 2012-11-21 | 200         | 102    | 20
我想要的是按日期分组的结果

$usage = $this->Environment->DailyCount->find('all',
array(
'conditions' =>array('DailyCount.customer_id' => $user['customer_id']),
'group' => array('DailyCount.date'),
'recursive'=>-1));
但它只返回一行,我想要全部三行

结果:

Array
(
    [0] => Array
        (
            [DailyCount] => Array
                (
                    [id] => 1
                    [date] => 2012-11-20
                    [customer_id] => 200
                    [env_id] => 100
                    [jobs] => 10
                )
       )

    [1] => Array
        (
            [DailyCount] => Array
                (
                    [id] => 4
                    [date] => 2012-11-21
                    [customer_id] => 200
                    [env_id] => 100
                    [jobs] => 10
                )

        )
)
在我的结果中,每个日期的所有三行都在后面-我需要所有三个环境id作业计数

任何人都可以帮助我如何在CakePHP中实现这一点,甚至如何在MySQL中获得这样的结果,我可以找到蛋糕的方法

-----------黑客解决方案------------

我无法回答自己的问题,因此编辑问题

好的,这不是一个完美的解决方案,但对我来说很有效,而且是一个快速的解决方案:

$dailyCount = $this->Environment->DailyCount->find('all',
array(
'conditions' => array('DailyCount.customer_id' => $user['customer_id']),
'order' => array('DailyCount.date DESC'),
'limit' => $limit,
'recursive'=>-1));

$usage = array();
foreach($dailyCount as $dc){
    $usage[$dc['DailyCount']['date']][] = $dc;
}

您可以使用MySQL的。如果要分组,这可能是最好的选择。

如果每个日期都需要全部3行,那么为什么要分组?使用您的示例表,您要查找的结果表是什么?您得到的结果是正确的,您按用户id所在的日期分组,因此应该有一条记录。结果是好的。问题在于您对cakephp结果数组的理解。