2016-06-21 226 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 ;