GROUP_CONCAT with JOINLEFT in Zend Db Select

橙三吉。 提交于 2019-12-10 04:12:08

问题


Assuming that I have 2 tables

articles
  id              title
  1               Article 1
  2               Article 2


Images
  id              article_id     image
  1               1              a.png
  2               1              b.png
  3               2              c.png
  4               2              d.png

All that I want is retreive all articles with their images.

For example:

article_id     title           images
1              Article 1       a.png, b.png
2              Article 2       c.png, d.png

How could I do that with Zend_Db_Select?

I tried something like this but had no luck:

$select = $this->getDbTable()->select()->setIntegrityCheck(false)->distinct();
$select->from(array('a'=>'articles'))
  ->joinLeft(array('i'=>'images'),'i.article_id=a.id',array('images'=> new
               Zend_Db_Expr('GROUP_CONCAT(i.image)')));

It returns just only 1 row which 'images' field contains images of both articles.

article_id     title           images
1              Article 1       a.png, b.png, c.png, d.png

What am i doing wrong here?


回答1:


You have not used group by clause in query.

Try below:

$select->from(array('a'=>'articles'))
  ->joinLeft(
       array('i'=>'images'),
       'i.article_id=a.id',
       array('images'=> new Zend_Db_Expr('GROUP_CONCAT(i.image)')))
  ->group('a.id');


来源:https://stackoverflow.com/questions/10086136/group-concat-with-joinleft-in-zend-db-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!