7

假设我有 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

我想要的只是检索所有带有图像的文章。

例如:

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

我怎么能用 Zend_Db_Select 做到这一点?

我尝试了这样的事情,但没有运气:

$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 行,其中“图像”字段包含两篇文章的图像。

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

我在这里做错了什么?

4

1 回答 1

9

您没有group by在查询中使用子句。

试试下面:

$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');
于 2012-04-10T09:41:18.393 回答