MySQL sum () Usage of SUM function

Source: Internet
Author: User

Name= the query in the record table? The value of the money added up
Sum with sum of function sums ()
Select SUM (Money) from record t where T.name =?
Other than that:
Count () to find the number of records
AVG () averaging
 MySQL sum () Usage of SUM function 

The syntax of the SUM function is:

Code to copy code as follows
SELECT SUM (expression)
From tables
WHERE predicates;

An expression can be a numeric field or a formula.

A simple example

For example, you may want to know that the total salary of the consolidated staff is more than USD, and its remuneration is 25,000/year

Code to copy code as follows
Select SUM (Salary) as "total salary"
From Employees
WHERE salary > 25000;

Used with the example using distinct

Code to copy code as follows
Select SUM (DISTINCT salary) as "total salary"
From Employees
WHERE salary > 25000;

Examples of working with formulas

The expression in the SUM function does not require a single field. You can also use a formula. For example, you may need net income for the business. Total revenue minus total net income calculated.

Code to copy code as follows
Select SUM (income-expenses) as "Net income"
From Gl_transactions;


You may also need to perform mathematical operations on the SUM function. For example, you might determine that the total Commission is 10% of the total sales.

Code to copy code as follows
Select SUM (Sales * 0.10) as "Commission"
From Order_Details;


With example using GROUP by

In some cases, you will be asked to use the SUM function of the first group.

For example, you can also use the SUM function to return the department name and total sales (related departments).

Code to copy code as follows
Select Department, SUM (sales) as "total sales"
From Order_Details
GROUP by department;

MySQL line multi-column sum sum () function2016-07-11 13:20 5492 People read comments (0) favorite reports Classification:MySQL (+)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The syntax of the SUM function is: SELECT SUM (expression) from tables WHERE predicates;

An expression can be a numeric field or a formula.

SELECT column 1+ column 2 + column 3 ... + column n as total from table

or select SUM (group_type+group_num_day+group_num_period+prize_pkg_id_owner) as total from Act_groupconfig;

MySQL sum () Usage of SUM function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.