Zend Db Select中具有JOINLEFT的GROUP_CONCAT [英] GROUP_CONCAT with JOINLEFT in Zend Db Select

查看:75
本文介绍了Zend Db Select中具有JOINLEFT的GROUP_CONCAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有2张桌子

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.

例如:

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

如何使用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)')));

仅返回1行,其中图片"字段包含两篇文章的图片.

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

我在这里做错什么了?

推荐答案

您尚未在查询中使用group by子句.

You have not used group by clause in query.

尝试以下方法:

$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');

这篇关于Zend Db Select中具有JOINLEFT的GROUP_CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆