I have a table of articles and I have a table of tags. There is an N: N relationship between them, so I also have a table called articles_tags_relations. I would like to get a list of articles with tags, however, in addition to the tag name, I also need its identifier. Thus, the returned data should be something like (of course, not in JSON format):
[[1,"FirstArticle",{"1":"FirstTag","2":"SecondTag"}],[2,"SecondArticle",{"3":"ThirdTag"}]]
So far, I have something like:
SELECT article.*, GROUP_CONCAT(tag.name SEPARATOR ', ') AS tags FROM articles AS article LEFT JOIN articles_tags_relations AS relation ON article.id = relation.article_id LEFT JOIN tags AS tag ON relation.tag_id = tag.id LIMIT 0,10;
But it has only tags without identifiers. There will be many read requests on the table, so performance is important. I'm not sure that using GROUP_CONCAT is the way to go, I am open to any ideas. Thank!
source
share