php - CakePHP 和 GROUP BY

标签 php mysql cakephp count group-by

使用 CakePHP 1.2,我正在尝试生成一个 GROUP BY 查询:

SELECT `categories`.*, COUNT(`entities`.id)
FROM `categories` 
LEFT JOIN `entities` ON (`categories`.`id` = `entities`.`category_id`)
GROUP BY `categories`.`id`

我会/应该怎么做?如果有帮助,我将使用“可包含”。

最佳答案

这就是我最终得到的:

 $options = array(
                    'conditions' => $conditions,
                    'fields'=>array('Category.*','COUNT(`Entity`.`id`) as `entity_count`'),
                    'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
                    'group' => '`Category`.`id`',
                    'contain' => array('Domain' => array('fields' => array('title')))
                );

                return $this->find('all', $options);

关于php - CakePHP 和 GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2929666/

相关文章:

php - 如何在 PHP 中调用值以供用户在表单(单选按钮和选择)中输入

php - 单元测试 cakephp 模型

mysql - 使用ODBC时,客户端软件和目标数据库之间会发生什么数据转换?

cakephp - CKEditor/CKFinder 与 CakePHP 2.0

php - 从 mysql 获取数据并以周为单位进行分组并显示日期

javascript - post值无法进入controller

php - 将查询错误转为 MySQLi 中的异常

php - @return 中有许多不同的类。如何避免将它们全部写下来?

php - MYSQL 添加约束错误

php - 如何在 php 中运行或加载 .po/.mo 文件进行本地化