Concatenate several fields into one with SQL

后端 未结 6 1777
再見小時候
再見小時候 2021-02-10 09:54

I have three tables tag, page, pagetag

With the data below

page

ID      NAME
1           


        
6条回答
  •  余生分开走
    2021-02-10 10:17

    Sergio del Amo:

    However, I am not getting the pages without tags. I guess i need to write my query with left outer joins.

    SELECT pagetag.id, page.name, group_concat(tag.name)
    FROM
    (
        page LEFT JOIN pagetag ON page.id = pagetag.pageid
    )
    LEFT JOIN tag ON pagetag.tagid = tag.id
    GROUP BY page.id;
    

    Not a very pretty query, but should give you what you want - pagetag.id and group_concat(tag.name) will be null for page 4 in the example you've posted above, but the page shall appear in the results.

提交回复
热议问题