以逗号分隔显示日期

时间:2012-02-22 09:58:01

标签: mysql date-format

我有这两张桌子。

Emp 
    id       name 
    01      Sten
    02      Willam


Customer

    id    name    date_entered     visited_by_Emp_id
    1     Reon     2011-12-11               1
    2     Reon     2011-12-13               1
    3     Samy     2012-01-02               2
    4     Samy     2012-01-05               2
    5     Samy    2012-02-07                2

现在我想用逗号分隔显示日期,如

我的输出应该是

Customer_Name    YEAR     Jan    Feb     Mar      Apr      Jun........ up to    Dec
  Samy           2012    02,05    07                                                 

  Reon           2011                                                          11,13

“所以我只想问一下如何用逗号分隔显示这些日期”, 任何建议都会有所帮助

1 个答案:

答案 0 :(得分:1)

试试这个 -

SELECT
  c.*,
  e.name,
  GROUP_CONCAT(IF(MONTH(date_entered) = 1, DAYOFMONTH(date_entered), NULL)) Jan,
  GROUP_CONCAT(IF(MONTH(date_entered) = 2, DAYOFMONTH(date_entered), NULL)) Feb,
  GROUP_CONCAT(IF(MONTH(date_entered) = 3, DAYOFMONTH(date_entered), NULL)) Mar
FROM emp e
  JOIN customer c
    ON e.id = c.visited_by_Emp_id
GROUP BY e.id, YEAR(date_entered)