Home  >  Article  >  Database  >  How to use sum() in mysql to find the sum of id fields?

How to use sum() in mysql to find the sum of id fields?

青灯夜游
青灯夜游Original
2020-10-20 16:48:013438browse

Mysql uses sum() to find the sum of the id field: use "SELECT SUM(id) FROM data table name;" to find the sum of the id field and return the sum of the id field values; SUM() function When calculating, ignore rows with NULL column values.

How to use sum() in mysql to find the sum of id fields?

(Recommended tutorial: mysql video tutorial)

The SUM() function returns the total number of numeric columns.

SQL SUM() Syntax

SELECT SUM(column_name) FROM table_name;

How does the SUM() function work?

  • If you use the SUM function in a SELECT statement that returns no matching rows, the SUM function returns NULL instead of 0.

  • The DISTINCT operator allows calculation of distinct values ​​in a collection.

  • The SUM function ignores NULL values ​​in calculations.

[Example] Calculate the sum of the id fields in the tb_text table. The input SQL statement and execution results are as follows.

mysql> SELECT SUM(id)
    -> AS sum
    -> FROM tb_text;
+-----------+
|    sum    |
+-----------+
|       942 |
+-----------+
1 row in set (0.00 sec)

As you can see from the query results, the sum of the id fields returned by the SUM() function is 942.

Tip: The SUM() function ignores rows with NULL column values ​​when calculating.

The above is the detailed content of How to use sum() in mysql to find the sum of id fields?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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