ZF2 - 子查询 [英] ZF2 - subqueries
本文介绍了ZF2 - 子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Zend Framework 2 中的子查询
Subquery in 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`
请帮忙.
谢谢,安吉斯
推荐答案
My required Query:
My required Query:
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 创建:
How I created using 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/
感谢所有回复.
这篇关于ZF2 - 子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文