I have three tables tag, page, pagetag
With the data below
page
ID NAME 1 page 1 2 page 2 3 page 3 4 page 4
tag
ID NAME 1 tag 1 2 tag 2 3 tag 3 4 tag 4
pagetag
ID PAGEID TAGID 1 2 1 2 2 3 3 3 4 4 1 1 5 1 2 6 1 3
I would like to get a string containing the correspondent tag names for each page with SQL in a single query. This is my desired output.
ID NAME TAGS 1 page 1 tag 1, tag 2, tag 3 2 page 2 tag 1, tag 3 3 page 3 tag 4 4 page 4
Is this possible with SQL?
I am using MySQL. Nonetheless, I would like a database vendor independent solution if possible.
Not a very pretty query, but should give you what you want –
pagetag.idandgroup_concat(tag.name)will benullfor page 4 in the example you’ve posted above, but the page shall appear in the results.