MySQL: 在group_concat中使用concat时如何排序?

MySQL: How to sort when using concat in group_concat?

我需要在 group_concat 内对 concat 中的 date 进行排序,而我的查询 运行 很好:

 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 ;

您应该在 group_concat 中使用它,而不是 concat:

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

您正在尝试提供 ORDER BY 子句作为 CONCAT(), which does not support that (mainly because it wouldn't make sense to sort a single value). The signature for GROUP_CONCAT() 的参数,这表明您必须将它们放在哪里:

GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

希望这会奏效

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