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`
请帮忙。
谢谢, 安吉斯
最佳答案
我需要的查询:
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`
我如何使用 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();
引用: http://eltonminetto.net/blog/2013/03/21/subqueries-no-zend-framework-2/
感谢大家的回复。
关于php - ZF2 - 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15742290/