Nấm*_*Lùn 7 mysql zend-framework left-join group-concat zend-db-select
假设我有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
Run Code Online (Sandbox Code Playgroud)
我想要的只是用他们的图像来回顾所有文章.
例如:
article_id title images
1 Article 1 a.png, b.png
2 Article 2 c.png, d.png
Run Code Online (Sandbox Code Playgroud)
我怎么能用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)')));
Run Code Online (Sandbox Code Playgroud)
它只返回1行'images'字段包含两篇文章的图像.
article_id title images
1 Article 1 a.png, b.png, c.png, d.png
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
小智 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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3163 次 |
| 最近记录: |