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();
it will return the result:
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"
Tarun singhal
source share