【发布时间】:2015-01-06 22:40:21
【问题描述】:
我有以下 SQL
SELECT articles.id, articles.title, tags.name AS tags
FROM articles
LEFT JOIN article_tag_association ON articles.id = article_tag_association.article_id
LEFT JOIN tags ON tags.id = article_tag_association.tag_id
这工作正常,除了它为文章的每个标签创建一行,这与限制混淆
例如
[
"0" => ["id" => "1", "title" => "test", "tags" => "tag1"],
"1" => ["id" => "1", "title" => "test", "tags" => "tag2"],
"2" => ["id" => "2", "title" => "test2", "tags" => "tag1"],
]
(只有 2 篇文章但三行)
有没有办法让它返回带有标签数组的每篇文章?
类似:
[
"0" => ["id" => "1", "title" => "test", "tags" => ["tag1", "tag2"]],
"1" => ["id" => "2", "title" => "test2", "tags" => ["tag1"]],
]
【问题讨论】:
-
你能发布你的表创建吗?可能更容易生成字符串格式的“数组”