cakephp包含聚合查询

时间:2012-03-06 18:05:25

标签: cakephp cakephp-1.3

在Cake中,我正在检索包含MIN / MAX / SUM / GROUP的数据集,但我也想要提取其他相关数据表(可包含)。这需要两个单独的请求,还是有办法构建一个蛋糕请求来处理这个?

1 个答案:

答案 0 :(得分:0)

取决于您想要追溯的内容。你能举个例子吗?还是更多细节?您可以使用JOINS方法之一,UNION或子查询

小例子:(这是来自cakephp book 2.0 ..)

$SubQueryCondition['`User2`.`status`'] = 'B';

$dbo = $this->User->getDataSource();
$subQuery = $dbo->buildStatement(
    array(
        'fields' => array('`User2`.`id`'),
        'table' => $dbo->fullTableName($this->User),
        'alias' => 'User2',
        'limit' => null,
        'offset' => null,
        'joins' => array(),
        'conditions' => $conditionsSubQuery,
        'order' => null,
        'group' => null
    ),
    $this->User
);
$subQuery = ' `User`.`id` NOT IN (' . $subQuery . ') ';
$subQueryExpression = $dbo->expression($subQuery);

$conditions[] = $subQueryExpression;

$this->User->find('all', compact('conditions'));