Anj*_*K P 2 php zend-framework2
Zend Framework 2中的子查询
我需要的查询:
SELECT `comment`.`id` AS `commentId`, `comment`.`comment` AS `comment`,
(SELECT COUNT(`comment_vote`.`id`) AS `negativeVote`
FROM `comment_vote`
WHERE vote = -1
AND `comment_vote`.`commentId` = `comment`.`id`) AS `nagetiveVoteCount`
FROM `comment`
Run Code Online (Sandbox Code Playgroud)
请帮忙.
谢谢,安吉斯
Anj*_*K P 15
我需要的查询:
SELECT `comment`.`id` AS `commentId`, `comment`.`comment` AS `comment`,
(SELECT COUNT(comment_vote.id) AS `negativeVote`
FROM `comment_vote`
WHERE vote = -1
AND comment_vote.commentId = comment.id) AS `nagetiveVoteCount`
FROM `comment`
Run Code Online (Sandbox Code Playgroud)
我是如何使用Zend Framework 2创建的:
$sql = new Sql($this->_adapter);
$mainSelect = $sql->select()->from('comment');
$selectPost = $sql->select()
->from('comment_vote')
->columns(array('negativeVote' => new \Zend\Db\Sql\Expression('COUNT(comment_vote.id)')))
->where('vote = -1')
->where('comment_vote.commentId = comment.id');
$mainSelect->columns(
array(
'commentId' => 'id', 'comment',
'nagetiveVoteCount' => new \Zend\Db\Sql\Expression('?', array($selectPost)),
)
);
$statement = $sql->prepareStatementForSqlObject($mainSelect);
$comments = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($comments);
return $resultSet->toArray();
Run Code Online (Sandbox Code Playgroud)
参考:http: //eltonminetto.net/blog/2013/03/21/subqueries-no-zend-framework-2/
感谢所有的回复.
| 归档时间: |
|
| 查看次数: |
5498 次 |
| 最近记录: |