PHP SUM/Total of values from a DB Query output
페이지 정보
본문
전체 개수 합하기
기본문법
SELECT SUM(실제필드명) AS 임시필드명 from 테이블명;
예1)
$sql = mysql_query(" select sum(car_value) as total from tblcars ");
$row = mysql_fetch_assoc($sql);
echo $row['total'];
예1-2)
$sql = "select sum(product1_price) as price1 from hp_order_data where order_idx = '". $order_idx ."'";
//echo $sql ."<br>";
$result = mysql_query($sql);
$total = mysql_result($result,0,0); // mysql_result 함수가 php 5.5 이후 삭제
echo $total;
예1-3)
$sql = "select count(*) as cnt from hp_order_data where order_idx = '". $order_idx ."'";
//echo $sql ."<br>";
$row = mysqli_fetch_array(mysqli_query($sql));
$total = $row['cnt'];
echo $total;
예2)
For example, you might wish to know how the combined total salary of all employees whose salary is above $25,000 / year.
SELECT SUM(salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
In this example, we've aliased the sum(salary) field as "Total Salary". As a result, "Total Salary" will display as the field name when the result set is returned.
예3) DISTINCT 사용
You can use the DISTINCT clause within the SUM function. For example, the SQL statement below returns the combined total salary of unique salary values where the salary is above $25,000 / year.
SELECT SUM(DISTINCT salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
If there were two salaries of $30,000/year, only one of these values would be used in the SUM function.
예4) Formula 사용
The expression contained within the SUM function does not need to be a single field. You could also use a formula. For example, you might want the net income for a business. Net Income is calculated as total income less total expenses.
SELECT SUM(income - expenses) as "Net Income"
FROM gl_transactions;
You might also want to perform a mathematical operation within a SUM function. For example, you might determine total commission as 10% of total sales.
SELECT SUM(sales * 0.10) as "Commission"
FROM order_details;
예5) GROUP BY 사용
In some cases, you will be required to use a GROUP BY clause with the SUM function.
For example, you could also use the SUM function to return the name of the department and the total sales (in the associated department).
SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in the SUM function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.
관련자료
http://bytes.com/topic/php/answers/866479-sum-total-values-db-query-output
http://www.techonthenet.com/sql/sum.php
http://www.technote.co.kr/php/technote1/board.php?board=apple&command=body&no=1119
기본문법
SELECT SUM(실제필드명) AS 임시필드명 from 테이블명;
예1)
$sql = mysql_query(" select sum(car_value) as total from tblcars ");
$row = mysql_fetch_assoc($sql);
echo $row['total'];
예1-2)
$sql = "select sum(product1_price) as price1 from hp_order_data where order_idx = '". $order_idx ."'";
//echo $sql ."<br>";
$result = mysql_query($sql);
$total = mysql_result($result,0,0); // mysql_result 함수가 php 5.5 이후 삭제
echo $total;
예1-3)
$sql = "select count(*) as cnt from hp_order_data where order_idx = '". $order_idx ."'";
//echo $sql ."<br>";
$row = mysqli_fetch_array(mysqli_query($sql));
$total = $row['cnt'];
echo $total;
예2)
For example, you might wish to know how the combined total salary of all employees whose salary is above $25,000 / year.
SELECT SUM(salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
In this example, we've aliased the sum(salary) field as "Total Salary". As a result, "Total Salary" will display as the field name when the result set is returned.
예3) DISTINCT 사용
You can use the DISTINCT clause within the SUM function. For example, the SQL statement below returns the combined total salary of unique salary values where the salary is above $25,000 / year.
SELECT SUM(DISTINCT salary) as "Total Salary"
FROM employees
WHERE salary > 25000;
If there were two salaries of $30,000/year, only one of these values would be used in the SUM function.
예4) Formula 사용
The expression contained within the SUM function does not need to be a single field. You could also use a formula. For example, you might want the net income for a business. Net Income is calculated as total income less total expenses.
SELECT SUM(income - expenses) as "Net Income"
FROM gl_transactions;
You might also want to perform a mathematical operation within a SUM function. For example, you might determine total commission as 10% of total sales.
SELECT SUM(sales * 0.10) as "Commission"
FROM order_details;
예5) GROUP BY 사용
In some cases, you will be required to use a GROUP BY clause with the SUM function.
For example, you could also use the SUM function to return the name of the department and the total sales (in the associated department).
SELECT department, SUM(sales) as "Total sales"
FROM order_details
GROUP BY department;
Because you have listed one column in your SELECT statement that is not encapsulated in the SUM function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.
관련자료
http://bytes.com/topic/php/answers/866479-sum-total-values-db-query-output
http://www.techonthenet.com/sql/sum.php
http://www.technote.co.kr/php/technote1/board.php?board=apple&command=body&no=1119
댓글목록
등록된 댓글이 없습니다.