Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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
CakePHP计数查询在phpmyadmin中运行时给出不同的结果_Cakephp - Fatal编程技术网

CakePHP计数查询在phpmyadmin中运行时给出不同的结果

CakePHP计数查询在phpmyadmin中运行时给出不同的结果,cakephp,Cakephp,我正在数据库上运行以下查询:- SELECT COUNT(*) AS COUNT, `Doctor`.`device_type` FROM `doctors` AS `Doctor` WHERE 1 = 1 GROUP BY `Doctor`.`device_type` 结果如下:- count device_type 47 Android 23 iPhone 而当以CakePHP查询的形式运行此查询时,其结果为“2”:- $this->Doctor->fin

我正在数据库上运行以下查询:-

SELECT COUNT(*) AS COUNT, `Doctor`.`device_type` FROM `doctors` AS `Doctor` WHERE 1 = 1 GROUP BY `Doctor`.`device_type` 
结果如下:-

count  device_type
47     Android
23     iPhone
而当以CakePHP查询的形式运行此查询时,其结果为“2”:-

$this->Doctor->find('count',array('group'=>'Doctor.device_type')); 

有人能告诉我为什么会发生这种情况吗?

CakePHP结果是正确的,因为它返回了查询返回的结果数。在您的示例中,有两行:“Android”和“iPhone”

find('count')
始终返回整数,而不是数组。蛋糕的作用基本上是这样的:-

$data = $this->Doctor->find('all', array('group' => 'Doctor.device_type'));
$count = count($data); // This is what find('count') will return.
您需要执行以下操作:-

$data = $this->Doctor->find('all', array(
    'fields' => array(
        'Doctor.device_type',
        'COUNT(Doctor.*) AS count'
    )
    'group' => 'Doctor.device_type'
));

@shweta太棒了!请接受这一正确答案,以帮助有类似问题的其他人。:-)我对此没有任何选择答案的左边应该有一个灰色的勾号(在分数计数器下面)。接受正确答案将提高您在堆栈溢出方面的声誉,因此当您收到问题的有效答案时,这是值得的。