Home > Database > SQL > body text

Detailed explanation of sum usage in mysql

下次还敢
Release: 2024-05-09 08:27:16
Original
867 people have browsed it

SUM() function calculates the sum of non-null values ​​in the specified column. It supports DISTINCT, filters, partial sums, and use with other aggregate functions. SUM() ignores NULL values, returns negative values, and returns NULL for non-numeric values.

Detailed explanation of sum usage in mysql

SUM() function in MySQL

SUM() function introduction

SUM() function is used to calculate the sum of all non-NULL values ​​in the specified column. It is an aggregate function that operates on a set of rows and returns a single value.

Syntax

<code class="sql">SUM(column_name)</code>
Copy after login

Where, column_name is the target column for which the sum is to be calculated.

Usage Example

The following is an example of using the SUM() function to calculate the sum of the sales columns in the table:

<code class="sql">SELECT SUM(sales)
FROM sales_table;</code>
Copy after login

NULL value handling

The SUM() function ignores NULL values. This means it only performs calculations on non-empty cells.

Distinct Keyword

You can use the DISTINCT keyword to exclude duplicate values. For example, the following query calculates the total sales of different customers in the table:

<code class="sql">SELECT SUM(DISTINCT sales)
FROM sales_table;</code>
Copy after login

Filter conditions

can be used in the SUM() functionWHERE clause to apply filter conditions. For example, the following query calculates the total sales of greater than $100 in the table:

<code class="sql">SELECT SUM(sales)
FROM sales_table
WHERE sales > 100;</code>
Copy after login

PARTS AND

You can use PARTITION BY and ORDER The BY clause groups the results and calculates partial sums. For example, the following query calculates the total sales for each customer, sorted by date:

<code class="sql">SELECT customer_id, SUM(sales)
FROM sales_table
GROUP BY customer_id
ORDER BY date;</code>
Copy after login

Other Notes

  • The SUM() function can be combined with other aggregations Used together with functions such as COUNT() and AVG().
  • If the target column contains negative values, the SUM() function returns negative values.
  • The SUM() function returns NULL if the target column contains non-numeric values.

The above is the detailed content of Detailed explanation of sum usage in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!