0

db - schema

I have a three table schema (see image above) Articles, Tags and ArticleTags. The ArticleTags table maps article_id to a tag_id. Im trying to retrieve all rows from Articles and join the tags associated with each row.

Barmar
  • 669,327
  • 51
  • 454
  • 560

1 Answers1

1
select a.article_id, group_concat(t.tag_name) as tag_names
from articles a
left join ArticleTags at on at.article_id = a.article_id
left join tags t on at.tag_id = t.tag_id
group by a.article_id 
juergen d
  • 195,137
  • 36
  • 275
  • 343
  • I forgot to add the query i'd been trying. similar to what you have. This query results in duplication of rows. I was trying a group_concat(a.article_id). Essentially I'm looking for an array of tags for each article_id. ``select a.*, t.tag_name, group_concat(a.article_id) from articles a left join ArticleTags at on at.article_id = a.article_id left join tags t on at.tag_id = t.tag_id`` – Anirvan Awatramani Aug 09 '17 at 13:51
  • worked like a charm! I was about to make two separate queries and manipulate the outputs in JS. – Anirvan Awatramani Aug 09 '17 at 16:24