Cake php嵌套表别名查询

时间:2015-05-11 05:11:54

标签: php cakephp pagination nested nested-query

对于此查询: -

SELECT `user_name` 
FROM `users`  
where `department` = (SELECT `department` FROM `users` WHERE `user_id` = 'xyz')

我能够从buildstatement

这样做

例如: -

$subQuery = $db->buildStatement(
                                    array(
                                        'fields'     => array('Users.department'),
                                        'table'      => $db->fullTableName($this->Users),
                                        'alias'      => 'Users',
                                        'limit'      => null,
                                        'offset'     => null,
                                        'joins'      => array(),
                                        'conditions' => $conditionsSubQuery,
                                        'order'      => null,
                                        'group'      => null
                                    ),
                                    $this->Users
                                );
$subQuery = 'Users.department = (' . $subQuery . ') ';

    $subQueryExpression = $db->expression($subQuery);

    $conditions[] = $subQueryExpression;

$this->Paginator->settings =    $conditions ;

由于查询是用cake php约定构建的,我能够直接使用cake php分页。

现在我正在尝试构建以下查询,但我没有得到

SELECT count(Users.department) FROM (SELECT `department` FROM `users`) AS `Users`   WHERE 1 = 1

我试过这种方式

$subQuery = $db->buildStatement(
                                    array(
                                        'fields'     => array('count(Users.department)'),
                                        'table'      => "(SELECT `department` FROM `users`)",
                                        'alias'      => 'Users',
                                        'limit'      => null,
                                        'offset'     => null,
                                        'joins'      => array(),
                                       /* 'conditions' => $conditionsSubQuery,*/
                                        'order'      => null,
                                        'group'      => null
                                    ),
                                    $this->Users
                                );

请帮我解决这个问题。 它将帮助我对所有其他嵌套查询使用cake php分页。

0 个答案:

没有答案