对于此查询:-
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 分页。