2016-02-25 75 views
0

我想返回註釋字段作爲一個字符串,並在開始日期例如如何使用GROUP_CONCAT DISTINCT CONCAT來自同一個表的兩個字段

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 

回答

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 
相關問題