如何使用GROUP_CONCAT DISTINCT从同一个表中CONCAT 2个字段

时间:2016-02-25 19:20:02

标签: php mysql

我想将注释字段作为一个字符串返回,例如开头的日期。

2015-01-13 Important Note!, 2014-05-04 Please Read

示例说明表:

  +----+---------+-----------------+---------------------+
  | id | user_id | note            | created_at          |
  +----+---------+-----------------+---------------------+
  |  2 |  2      | Important Note! | 2015-01-13 18:28:01 |
  |  3 |  2      | Please Read     | 2014-05-04 11:18:52 |
  |  4 |  3      | Who Left This?  | 2011-01-19 12:14:57 |
  +----+---------+-----------------+---------------------+

我有以下SQL但我需要在created_at列中包含日期部分:

SELECT u.name, GROUP_CONCAT(DISTINCT n.note) AS notes
FROM user u
LEFT JOIN notes n ON n.user_id = u.id
GROUP BY u.id

1 个答案:

答案 0 :(得分:0)

刚刚解决了这个问题:

SELECT u.name, GROUP_CONCAT(DISTINCT DATE_FORMAT(n.created_at, '%e %b %y'), ' ', n.note) AS notes
FROM user u
LEFT JOIN notes n ON n.user_id = u.id
GROUP BY u.id