dopa53272 2017-02-14 06:07
浏览 44
已采纳

如何在zend frame work2中使用左连接编写计数查询?

Hi I am new to Zend Framework. In normal php I know how to write queries. But in Zend Framework this is totally different.

I have written the query with left join and it is working fine but the thing is I want to get count of the joined table id. I will show my controller code:

controller:

$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$projectTable = new TableGateway('blog', $adapter);
$rowset = $projectTable->select(
    function(Select $select) {       
        $select->join(
            array('b' => 'blog_answers'),
            "b.question_id = blog.id",
            array('new'=>COUNT('b.question_id'),'left')
        ); 
    }
);

while running this i am getting can you please clarify this how get the count.

  • 写回答

1条回答 默认 最新

  • doulv8162 2017-02-15 08:43
    关注

    HI this is the correct Query i got.

    `
    $adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
            $projectTable = new TableGateway('blog', $adapter);
            $rowset = $projectTable->select(function(Select $select) {
           $select->columns(array(
            '*',
            'num' => new Expression('COUNT(blog_answers.question_id)')
        ));
            $select->join('blog_answers', 'blog.id = blog_answers.question_id', array(), 'left');
             $select->group('blog.id');     
                });`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?