php - ZF2 - 子查询

标签 php zend-framework2

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/

相关文章:

php - AngularJS ngRoute 和 PHP $_SESSION 变量

events - 事件管理器附加通配符

php - ZendFramework 2 - 表单 - 具有多个无效电子邮件地址的电子邮件字段导致 "Array to string conversion"

php - 我需要设置 ini_set( 'default_charset' , 'UTF-8' ); 吗?

php - ffmpeg 手动工作,但不能与 cron 一起工作?

php - Zend Framework 2 数据库连接问题

doctrine-orm - 表格已经存在

wordpress - 如何使用 wordpress 运行 zf2 ..?

linux - 在 PHP CLI 中设置 max_execution_time

PHP : Error: Wrong parameters for Exception with thrown Array Exception