Concatenate several fields into one with SQL

后端 未结 6 1800
再見小時候
再見小時候 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:20

    Yep, you can do it across the 3 something like the below:

    SELECT page_tag.id, page.name, group_concat(tags.name)
    FROM tag, page, page_tag
    WHERE page_tag.page_id = page.page_id AND page_tag.tag_id = tag.id;
    

    Has not been tested, and could be probably be written a tad more efficiently, but should get you started!

    Also, MySQL is assumed, so may not play so nice with MSSQL! And MySQL isn't wild about hyphens in field names, so changed to underscores in the above examples.

提交回复
热议问题