SUM/Total of values from a DB Query output > 기술자료 | 해피정닷컴

SUM/Total of values from a DB Query output > 기술자료

본문 바로가기

사이트 내 전체검색

SUM/Total of values from a DB Query output > 기술자료

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

댓글목록

등록된 댓글이 없습니다.


Total 195건 5 페이지
  • RSS
기술자료 목록
115
PHP   22769  2013-08-22 13:35 ~ 2021-08-20 19:00  
114
PHP   21381  2013-06-20 05:00  
113
PHP   17877  2013-06-19 00:29  
112
PHP   23151  2013-05-30 21:27  
111
PHP   18566  2013-05-28 14:27  
110
PHP   17048  2013-05-16 13:13  
109
PHP   17598  2013-05-11 21:40  
108
PHP   17337  2013-04-17 23:13 ~ 2021-05-18 16:40  
107
PHP   32404  2013-04-16 02:21 ~ 2020-02-11 20:22  
106
PHP   16935  2013-03-26 01:49  
105
PHP   18744  2013-02-20 12:09 ~ 2021-07-15 07:19  
104
PHP   19546  2012-10-28 23:34  
103
PHP   18215  2012-06-13 03:34  
102
PHP   45883  2012-04-05 00:46  
101
PHP   42776  2012-04-04 23:04  
열람
PHP   16892  2012-04-04 22:15 ~ 2021-01-18 12:04  
99
PHP   28934  2012-03-31 17:48 ~ 2018-12-17 14:52  
98
PHP   18648  2012-03-31 11:03  
97
PHP   92483  2012-03-26 22:22  
96
PHP   52059  2012-03-24 22:40 ~ 2012-03-31 00:00  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2024 해피정닷컴. All Rights Reserved.