2016-06-21 227 views
2

我需要在concatgroup_concat排序date而我的查询精运行:MySQL:如何在group_concat中使用concat时进行排序?

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

+0

当我使用CONCAT然后才能通过努力站给出错误 – skhurams

+1

'CONCAT'是一个函数,你不能在函数中使用子句(除了某些窗口函数)。你必须在CONCAT()函数的OUTSIDE外面进行排序。此外,在GROUP_CONCAT中不能有多个'ORDER BY'子句。 – Pred

回答

4

你应该group_concat使用它,而不是concat

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

您正在尝试提供ORDER BY条款作为参数CONCAT(),它不支持(主要因为排序单个值没有意义)。这就是你有GROUP_CONCAT()显示签名把他们:

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

希望这将工作

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 ;