加入子查询Zend Framework 2

时间:2016-09-08 02:37:47

标签: php mysql zend-framework

需要帮助将MySQL转换为zf2 TableGateway SQL

Mysql查询:

select t1.* from response t1
inner join 
(select activity_id, max(submitted) as recent from response where learner_id = '<USER_ID>' group by activity_id) t2
on t1.activity_id = t2.activity_id
where t1.submitted = t2.recent
order by activity_id;

我想出了ZF2查询:

    $rowset = $this->tableGateway->select(function (Select $select) use ($learnerId)
    {
        $inner = new \Zend\Db\Sql\Select (array('t2'=>'response'));
         $inner ->columns(array('activity_id'),array('recent' => new \Zend\Db\Sql\Expression("max(submitted)")))
                ->where('learner_id ?', $learnerId)
                ->group('activity_id');      

$select
         ->join(array('t1' => $inner),
                     't1.activity_id = t2.activity_id')
         ->where('submitted = submitted')
         ->order('activity_id');
    });

错误:

  

无法执行声明(42S22 - 1054 - 未知列&#39; t2.activity_id&#39; on&#39; on&#39;)

0 个答案:

没有答案