我寫了下面的查詢,它是設計乘以產品的總數量,與數量價格並把它放到一個新列中。我在想什麼,有點卡住了。那麼有沒有辦法來總計這個新的專欄。因此,我還可以獲得訂購每行的總額,我也可以獲得訂單本身的總額。MySQL查詢問題 - 添加額外和總計
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
我寫了下面的查詢,它是設計乘以產品的總數量,與數量價格並把它放到一個新列中。我在想什麼,有點卡住了。那麼有沒有辦法來總計這個新的專欄。因此,我還可以獲得訂購每行的總額,我也可以獲得訂單本身的總額。MySQL查詢問題 - 添加額外和總計
SELECT Product_Quantity, ProductPrice, (Product_Quantity*ProductPrice) as Total FROM Order_Info WHERE Order_Number = '1'
這添加了一個記錄總數。 可模擬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'
酷酷的人。我已經完成了應用程序端(PHP)的總計。感謝你的幫助。 – Trotterwatch
爲了得到一個摘要行,你可以使用SQL的union
和sum
聚合函數。試試這個:
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'
你是說整個專欄的總數? –
嗨穆罕默德,是的隊友。 – Trotterwatch
您可以使用ROLLUP,但我可能會在應用程序代碼 – Strawberry