MySQL Limit with Many to Many Relationship

后端 未结 4 669
傲寒
傲寒 2021-02-06 18:43

Given a SCHEMA for implementing tags

ITEM ItemId, ItemContent

TAG TagId, TagName

ITEM_TAG ItemId, TagId

What is the best way to limit the number

4条回答
  •  清歌不尽
    2021-02-06 19:06

    My second solution uses a MySQL function GROUP_CONCAT() to combine all tags matching the item into a comma-separated string in the result set.

    SELECT i.ItemContent, GROUP_CONCAT(t.TagName ORDER BY t.TagName) AS TagList
    FROM item AS i 
      INNER JOIN ItemTag AS it ON i.id = it.ItemId 
      INNER JOIN tag AS t ON t.id = it.TagId
    GROUP BY i.ItemId;
    

    The GROUP_CONCAT() function is a MySQL feature, it's not part of standard SQL.

提交回复
热议问题