2017-02-18 60 views
0

我寫了下面的查詢,它是設計乘以產品的總數量,與數量價格並把它放到一個新列中。我在想什麼,有點卡住了。那麼有沒有辦法來總計這個新的專欄。因此,我還可以獲得訂購每行的總額,我也可以獲得訂單本身的總額。MySQL查詢問題 - 添加額外和總計

SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1' 
+0

你是說整個專欄的總數? –

+0

嗨穆罕默德,是的隊友。 – Trotterwatch

+0

您可以使用ROLLUP,但我可能會在應用程序代碼 – Strawberry

回答

2

這添加了一個記錄總數。 可模擬GROUP BY與ROLLUP

SELECT 
    Product_Quantity 
, ProductPrice 
, (Product_Quantity*ProductPrice) AS Total 
FROM 
Order_Info 
WHERE 
Order_Number = '1' 

UNION ALL 

SELECT 
    NULL 
, NULL 
, SUM((Product_Quantity*ProductPrice)) AS Total 
FROM 
Order_Info 
WHERE 
Order_Number = '1' 
+0

酷酷的人。我已經完成了應用程序端(PHP)的總計。感謝你的幫助。 – Trotterwatch

1

爲了得到一個摘要行,你可以使用SQL的unionsum聚合函數。試試這個:

SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1' 
union all 
SELECT sum(Product_Quantity), sum(ProductPrice), sum(Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'