2015-11-02 106 views
0

我試圖從SUMED列使用下面的查詢基於我的查詢記錄進行排序:MySQL的排序不排序正確的數據

SELECT o.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS 'name', 
    COUNT(o.id_customer) AS 'repeat', FORMAT(SUM(o.total_products),2) AS 'amount' 
FROM ps_orders AS o 
LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer 
WHERE o.current_state IN (2,4,5) 
AND o.invoice_date BETWEEN '2015-01-01' AND '2015-12-31' 
GROUP BY o.id_customer 
ORDER BY amount DESC 

上面的查詢沒有返回正確的數據,更具體返回的記錄未正確排序。

任何人都可以指向正確的方向嗎?任何幫助深表感謝。

乾杯

+1

你期待什麼,你會得到什麼?你能舉一個例子嗎? – Jacob

回答

1

您格式化量與FORMAT方法

FORMAT返回的字符串。

您的查詢由數據類型的字符串,而不是數量

+0

我稍後會接受你的回答。你說得對,我不應該使用FORMAT。 – Jeremy

0

你可以嘗試使用子查詢:

SELECT * 
FROM (
    SELECT o.id_customer, CONCAT(c.firstname, ' ', c.lastname) AS 'name', 
     COUNT(o.id_customer) AS 'repeat', FORMAT(SUM(o.total_products),2) AS 'amount' 
    FROM ps_orders AS o 
    LEFT JOIN ps_customer AS c ON o.id_customer = c.id_customer 
    WHERE o.current_state IN (2,4,5) 
    AND o.invoice_date BETWEEN '2015-01-01' AND '2015-12-31' 
    GROUP BY o.id_customer 
) AS o 
ORDER BY amount DESC