我在我的数据库上运行以下查询: -
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'));
任何人都可以建议为什么会这样吗?
答案 0 :(得分:0)
CakePHP结果是正确的,因为它返回查询返回的结果数量的计数。在您的情况下,您有2行:'Android'和'iPhone'。
find('count')
始终返回一个整数,而不是数组。 Cake正在做的基本上是这样的: -
$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'
));