2014-01-22 4 views
-2

어리석은지도 모르지만 2 개의 필드를 곱하고 AS 함수를 사용하여 임시 필드를 만들었습니다. 그것을 요약하기 위해 임시 필드의 값을 저장해야합니다.mySQL의 변수에 AS 함수의 값을 저장하는 방법은 무엇입니까?

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

그래서 totalProfit에게

어떤 아이디어를 (내가 MySQL로 새로운 해요) 요약해야합니까? 그냥 모든 트랜잭션에 걸쳐 총 이익을 얻으려고 노력하는 경우

답변

1

, 당신은이 작업을 수행 할 수 있습니다

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

당신은 할 수 있어야한다 그냥 수행

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

덕분에 너무 작품! – user3224964

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

당신 그룹 함수와 함께 sum 함수를 사용할 수 있습니다.

의 당신이 모든 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; 
관련 문제