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,634건 77 페이지
  • RSS
기술자료 목록
1114
ClassicASP   29119  2012-04-17 22:22 ~ 2018-07-10 13:40  
1113
SNS   13878  2012-04-17 21:59  
1112
ClassicASP   13859  2012-04-17 13:55  
1111
Editor   24484  2012-04-16 23:44  
1110
전자결제   13349  2012-04-16 04:35 ~ 2015-03-18 00:00  
1109
그누보드   14821  2012-04-13 13:27  
1108
Editor   34896  2012-04-13 06:02  
1107
WindowsServer   18100  2012-04-12 23:59  
1106
WindowsServer   47370  2012-04-12 23:49  
1105
전자결제   23868  2012-04-09 23:39 ~ 2021-11-19 11:19  
1104
영카트   17129  2012-04-07 14:13 ~ 2015-01-23 00:00  
1103
JavaScript   15408  2012-04-06 21:01  
1102
PHP   45865  2012-04-05 00:46  
1101
PHP   42745  2012-04-04 23:04  
열람
PHP   16869  2012-04-04 22:15 ~ 2021-01-18 12:04  
1099
그누보드   12965  2012-04-30 22:42  
1098
그누보드   9204  2013-04-29 14:50  
1097
JavaScript   19158  2012-04-02 15:14  
1096
ClassicASP   20891  2012-04-02 09:26  
1095
ClassicASP   15167  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.