0

我现在几乎尽我所能来解决他的以下问题。到目前为止没有成功。但必须有一个解决方案,因为我不认为这个案子太特别了。我想我只是一个该死的初学者;-)我需要在 MySQL 中加入 union merge 或其他什么;-) 来解决以下问题......

案例:三个表“posts”、“tagmap”和“tags”

tagmap表存储所有 nm 关系,posts 的 id 和 tags 的id tags存储tag_nametag id posts 表存储post_titlepost_id

 posts    post_id    post_title
          1          Charlie Chaplin Painting

 tagmap   id         post_id    tag_id
          100        1          12
          101        1          13
          102        1          14

 tags     tag_id     tag_name
          12         Acryl
          13         Chalk
          14         Poster

我想要实现的是获得一个结果,即所有相关标签都合并在一列中。通过逗号分隔的列表或空格:
post_id => 1, post_title => Charlie Chaplin... tag_name => Acryl, Chalk, Poster

但到目前为止,我唯一能得到的是这样的多重结果:
post_id => 1,post_title => Charlie Chaplin... tag_name => Acryl
post_id => 1,post_title => Charlie Chaplin... tag_name => Chalk
post_id => 1, post_title => 查理卓别林... tag_name => 海报

有谁知道如何实现这一点...任何帮助将不胜感激,并提前感谢所有可以帮助我的人;-)

4

1 回答 1

2

采用:

  SELECT p.post_id,
         p.post_title,
         GROUP_CONCAT(t.tag_name ORDER BY t.tag_name SEPARATOR ', ')
    FROM POSTS p
    JOIN TAGMAP tm ON tm.post_id = p.post_id
    JOIN TAGS t ON t.tag_id = tm.tag_id
GROUP BY p.post_id, p.post_title

参考:

于 2010-01-19T18:33:02.270 回答