使用GROUP_CONCAT选择多对多关系

时间:2021-03-03 19:19:00

i have 3 tables post,tags,tags_relationship

我有3个表post,标签,tags_relationship

post:
  post_id, post_title....
tags:
  tag_id, tag_name...
tags_relationship:
 tag_rel_id, tag_rel_post_id, tag_rel_tag_id

how do i select all of the posts with tags in single column with (tag1,tag2,...) format for each post via the tags_relationship table?

如何通过tags_relationship表为每个帖子选择具有单个列标记(tag1、tag2、…)格式的所有帖子?

1 个解决方案

#1


5  

Answer, so the question can be closed.

回答,这样问题就可以结束了。

SOLUTION FOUND

发现解决方案


SELECT 
    p.*, 
    GROUP_CONCAT(t.tag_name) tagged 
FROM 
    tags_relation tr   
    INNER JOIN posts p ON p.post_id = tr.rel_post_id   
    INNER JOIN tags t ON t.tag_id = tr.rel_tag_id 
GROUP BY 
    p.post_id 

#1


5  

Answer, so the question can be closed.

回答,这样问题就可以结束了。

SOLUTION FOUND

发现解决方案


SELECT 
    p.*, 
    GROUP_CONCAT(t.tag_name) tagged 
FROM 
    tags_relation tr   
    INNER JOIN posts p ON p.post_id = tr.rel_post_id   
    INNER JOIN tags t ON t.tag_id = tr.rel_tag_id 
GROUP BY 
    p.post_id