mysql GROUP_CONCAT DISTINCT multiple columns

后端 未结 2 1088
暖寄归人
暖寄归人 2020-12-13 12:42

I have a tag field for a blog posts. tags have unique id but their displayName might be duplicated. What I want is a query that selects posts and in all_tags fi

相关标签:
2条回答
  • 2020-12-13 13:16

    As advised by @Willa, I add my comment as an anwser :

    GROUP_CONCAT allows you to concat multiple fields :

    GROUP_CONCAT(tags.id, ',', tags.displayName)
    

    The only difference with Stephan's answer is in case your code allows the same tag to be affected several times to one post OR if you JOIN sequence leads you to multiselect the same record in the tag table. In those case, my solution will return the same tags multiple times.

    0 讨论(0)
  • 2020-12-13 13:17

    Try this:

    GROUP_CONCAT(
      DISTINCT CONCAT(tags.id,',',tags.displayName) 
      ORDER BY posts.id 
      SEPARATOR ';'
    )
    
    0 讨论(0)
提交回复
热议问题