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 2,641건 77 페이지
  • RSS
기술자료 목록
1121
ClassicASP   29064  2012-04-17 22:22 ~ 2018-07-10 13:40  
1120
SNS   13860  2012-04-17 21:59  
1119
ClassicASP   13840  2012-04-17 13:55  
1118
Editor   24452  2012-04-16 23:44  
1117
전자결제   13324  2012-04-16 04:35 ~ 2015-03-18 00:00  
1116
그누보드   14796  2012-04-13 13:27  
1115
Editor   34821  2012-04-13 06:02  
1114
WindowsServer   18077  2012-04-12 23:59  
1113
WindowsServer   47290  2012-04-12 23:49  
1112
전자결제   23842  2012-04-09 23:39 ~ 2021-11-19 11:19  
1111
영카트   17113  2012-04-07 14:13 ~ 2015-01-23 00:00  
1110
JavaScript   15382  2012-04-06 21:01  
1109
PHP   45829  2012-04-05 00:46  
1108
PHP   42720  2012-04-04 23:04  
열람
PHP   16833  2012-04-04 22:15 ~ 2021-01-18 12:04  
1106
그누보드   12947  2012-04-30 22:42  
1105
그누보드   9189  2013-04-29 14:50  
1104
JavaScript   19131  2012-04-02 15:14  
1103
ClassicASP   20866  2012-04-02 09:26  
1102
ClassicASP   15135  2012-04-01 17:00  

검색

해피정닷컴 정보

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

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