如何在mySQL的变量中存储AS函数的值?

时间:2014-01-22 19:22:08

标签: mysql sum

可能是愚蠢的,但我将2个字段相乘,并使用AS函数创建了一个临时字段。需要存储临时字段的值来总结它。

select branchNo,prodCode, prodQty, prodPrice, prodQty * prodPrice AS totalProfit
from transaction WHERE branchNo = 14;

所以我需要总结一下totalProfit

任何想法(我是MySQL新手)?

4 个答案:

答案 0 :(得分:1)

如果您只是想在所有交易中获得总利润,可以这样做:

select SUM(prodQty * prodPrice) AS totalProfit
from transaction WHERE branchNo = 14;

答案 1 :(得分:0)

你应该能够做到:

select sum(prodQty * prodPrice) AS sumtotalProfit
from transaction 
WHERE branchNo = 14;

答案 2 :(得分:0)

SELECT SUM(`prodQty` * `prodPrice`) AS totalProfit FROM `transaction` WHERE `branchNo` = 14;

答案 3 :(得分:0)

您可以将sum函数与group by一起使用。

假设你想知道每个prodCode和branchNo 14的总和:

select `prodCode`, SUM(`prodQty` * `prodPrice`) as `totalProfit` 
from `transaction` 
where `branchNo` = 14 
group by `prodCode`;

如果您只想使用总和:

select SUM(`prodQty` * `prodPrice`) as `totalProfit` 
from `transaction` 
where `branchNo` = 14;