Home  >  Article  >  Database  >  How to sum columns in mysql

How to sum columns in mysql

青灯夜游
青灯夜游Original
2022-06-20 17:16:288350browse

In mysql, you can use the SELECT statement with the SUM() function to sum the columns, which can return the sum of the specified column values. The sum syntax is "SELECT SUM(DISTINCT column name) FROM table name;" ; Among them, the "DISTINCT" operator allows to calculate different values ​​in the set and can be omitted. The SUM() function ignores rows with NULL column values ​​when calculating.

How to sum columns in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

In mysql, you can use the SUM() function to sum columns.

SUM() is a sum function that returns the sum of the specified column values. Syntax:

SELECT SUM(DISTINCT 列名) FROM 表名;
  • The DISTINCT operator allows calculation of distinct values ​​in a set

Description:

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

  • The SUM() function will ignore rows with NULL column values ​​when calculating

mysql SUM() calculates columns and example

Create a student score table tb_students_score

CREATE TABLE tb_students_score
(
id INT(11),
student_name VARCHAR(25),
student_score INT(11)
);

How to sum columns in mysql

After inserting the data, view the data

How to sum columns in mysql

Use the sum() function to calculate the total score of student grades in the tb_students_score table

SELECT SUM(student_score) FROM tb_students_score;

How to sum columns in mysql

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to sum columns in mysql. 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