使用包含GROUP_CONCAT的Zend DB编写子查询

时间:2016-06-02 17:58:00

标签: php mysql zend-framework zend-db

我有问题如何将复杂的SELECT重写为Zend语法。 SELECT是SUBSELECT,GROUP_CONCAT使它更复杂。

Sorted Merge

我尝试将纯SELECT插入查询

select * from Atrybutypowiazania as ap
left join (
    select a.id_gr,
    group_concat(a.nazwa SEPARATOR ', ') 
    from Atrybutygrupy as ag
    left join Atrybuty a on a.id_gr = ag.id
    where ag.jedn_miary = 'rozmiar'
    group by ag.id 
) qqq on qqq.id_gr = ap.id_gr
left join Produkty p on p.id = ap.id_og
where qqq.id_gr <> ''
group by ap.id_og

但它不起作用

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

$sql = $this->db->select()
        ->from(array('ag' => 'Atrybutygrupy'), array('idgr' => 'a.id_gr', 'myRozmiary' => new Zend_Db_Expr('GROUP_CONCAT(a.nazwa SEPARATOR ", ")')))
        ->joinleft(array('a' => 'Atrybuty'), 'a.id_gr = ag.id' )
        ->where($where1)
        ->group('ag.id');

$select = $this->db->select()
        ->from(array('ap' => 'Atrybutypowiazania'))
        ->joinleft(array('qqq' => $sql), 'qqq.id_gr = ap.id_gr')
        ->joinleft(array('p' => 'Produkty'), 'p.id = ap.id_og')
        ->where($where)
        ->group('ap.id_og')
        ;

$result = $this->db->fetchAll($select);