Search code examples
mysqlsqlgroup-concat

MySQL: How to sort when using concat in group_concat?


I need to sort date in concat within group_concat while my query is running fine:

 SELECT 
    report.`Name`,
    GROUP_CONCAT(
      CONCAT(
        "[",
      DATE(report.Date) --(not working) order by DATE(report.Date)  ,
        ',',
      report.ProductPrice --(not working) order by DATE(report.ProductPrice) ,
        "]"
      ) 
    ) AS ProductPrice  
  FROM report
 GROUP BY report.Name ;

enter image description here


Solution

  • You should use it in group_concat, not concat:

    group_concat(
      concat('[', date(report.Date), ',', report.ProductPrice, ']') 
      order by date(report.Date) desc
    )