Solution for the above query in ZF2:
$sub = new Select('comment_vote');
$sub->columns(array('negativeVote' => new \Zend\Db\Sql\Expression('COUNT(comment_vote.id)')), FALSE)->where(array('vote' => -1 , 'comment_vote.commentId' => 'comment.id'));
$subquery = new \Zend\Db\Sql\Expression("({$sub->getSqlString()})");
$predicate = new \Zend\Db\Sql\Predicate\Expression("({$sub->getSqlString()})");
$sql = new Sql($this->adapter);
$select = $sql->select()->from('comment');
$select->columns(array('commentId','comment', 'nagetiveVoteCount' => $subquery));
echo $select->getSqlString();
它將返回的輸出:
SELECT "comment"."commentId" 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"
我們能看到你試過到目前爲止的代碼? – Rikesh