php - ZF2 - subqueries -
subquery in zend framework 2
my required query:
select `comment`.`id` `commentid`, `comment`.`comment` `comment`, (select count(`comment_vote`.`id`) `negativevote` `comment_vote` vote = -1 , `comment_vote`.`commentid` = `comment`.`id`) `nagetivevotecount` `comment`
please help.
thanks, anjith
my required query:
select `comment`.`id` `commentid`, `comment`.`comment` `comment`, (select count(comment_vote.id) `negativevote` `comment_vote` vote = -1 , comment_vote.commentid = comment.id) `nagetivevotecount` `comment`
how 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();
refrence: http://eltonminetto.net/blog/2013/03/21/subqueries-no-zend-framework-2/
thanks responses.
Comments
Post a Comment