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,643건 77 페이지
  • RSS
기술자료 목록
1123
etc언어   24173  2012-04-18 18:32 ~ 2016-11-07 00:00  
1122
ClassicASP   15517  2012-04-18 13:41 ~ 2016-03-30 00:00  
1121
ClassicASP   30501  2012-04-17 22:22 ~ 2018-07-10 13:40  
1120
SNS   14079  2012-04-17 21:59  
1119
ClassicASP   14093  2012-04-17 13:55  
1118
Editor   24833  2012-04-16 23:44  
1117
전자결제   13552  2012-04-16 04:35 ~ 2015-03-18 00:00  
1116
그누보드   15048  2012-04-13 13:27  
1115
Editor   35450  2012-04-13 06:02  
1114
WindowsServer   18424  2012-04-12 23:59  
1113
WindowsServer   47942  2012-04-12 23:49  
1112
전자결제   24140  2012-04-09 23:39 ~ 2021-11-19 11:19  
1111
영카트   17388  2012-04-07 14:13 ~ 2015-01-23 00:00  
1110
JavaScript   15607  2012-04-06 21:01  
1109
PHP   46085  2012-04-05 00:46  
1108
PHP   43016  2012-04-04 23:04  
열람
PHP   17594  2012-04-04 22:15 ~ 2021-01-18 12:04  
1106
그누보드   13089  2012-04-30 22:42  
1105
그누보드   9308  2013-04-29 14:50  
1104
JavaScript   19401  2012-04-02 15:14  

검색

해피정닷컴 정보

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

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