GROUP_CONCAT多个列作为数组或可扩展的字符串

时间:2014-12-20 18:04:43

标签: php mysql

声明我想出了:

   SELECT p.user_id AS started_by,
          p.position,
          p.created AS started_time,
          GROUP_CONCAT(
            c.id,
            c.user_id,
            c.comment,
            c.created) AS comments
     FROM pointers AS p
     JOIN comments AS c
       ON p.id = c.pointer_id
    WHERE p.archive_id = 3
 GROUP BY p.id 

它让我:

enter image description here

GROUP_CONCAT()简单地连接c.id会发生什么。 c.user_id c.comment和c.created。我甚至无法以任何有意义的方式爆炸这个价值。 (注意日期和id如何一起作为1个字符串)。

我不想为评论创建单独的查询。或者我应该吗?

无论如何,我需要通过c.created评论来订购,并且有两个分隔符。例如:

comments => 1,1,text this is!,2014-12-20 17:52:02;3,1,asasd,2014-12-20 20:46:40

1 个答案:

答案 0 :(得分:0)

如何为列的连接定义分隔符,为组本身定义另一个分隔符

GROUP_CONCAT(concat_ws(',',
                       c.id,
                       c.user_id,
                       c.comment,
                       c.created)
            separator ';') AS comments