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.
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) );
After inserting the data, view the data
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;
[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!