我在Mysql中有3个表:
Products with columns |ID|Name|,
Tags with columns also |ID|Name| and the third one is
ProductTagsRelations with |ID|prodID|tagID|.
第三个包含带有产品 ID 的行以及它们链接到的每个标签。
我能否从“产品”表中获取所有数据,以及与“标签”表中的每个标签相关的所有数据(以单个字符串或其他形式),全部在一个查询中吗?
最佳答案
您可以通过连接表格并按产品分组来实现:
select
p.id, p.name,
group_concat(t.name order by t.name) tags
from Products p
left join ProductTagsRelations r on r.prodid = p.id
left join Tags t on t.id = r.tagid
group by p.id, p.name
与 group_concat()
您将获得每个产品的标签列表。
请参阅demo .
关于mysql - 从表中获取数据以及从另一个表中连接的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157931/