Home > Database > Mysql Tutorial > body text

How to sum columns in mysql

青灯夜游
Release: 2022-06-21 17:57:58
Original
8372 people have browsed it

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 表名;
Copy after login
  • 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)
);
Copy after login

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;
Copy after login

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!

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