Home > Database > Mysql Tutorial > body text

How to implement group sum in mysql

青灯夜游
Release: 2022-06-23 16:49:01
Original
12876 people have browsed it

Implementation method: 1. Use the SELECT statement to query the data in the specified table; 2. Use the "GROUP BY" keyword to group the query results according to one or more fields; 3. Use the SUM() function according to In the case of grouping, the sum of the specified fields in different groups is returned. The syntax is "SELECT SUM (field name for summation) FROM table name GROUP BY field name to be grouped;".

How to implement group sum in mysql

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

In mysql, you can use the SELECT statement, the "GROUP BY" keyword and the SUM() function to implement group summation.

  • The SELECT statement can query the data in the specified table

  • "GROUP BY" key The query results can be grouped according to one or more fields

  • The SUM() function returns the sum of the specified fields in different groups according to the grouping situation

SELECT SUM(进行求和的字段名) FROM 表名 GROUP BY 需要进行分组的字段名;
Copy after login

Example:

Perform a group query on the sex field of the data table mip_demo, and then group the results of boys and girls in the score field sum.

Let’s first take a look at the data in the mip_demo table:

SELECT * FROM mip_demo;
Copy after login

How to implement group sum in mysql

Find the sum of the scores of boys and girls in groups.

SELECT sex,SUM(score) FROM mip_demo GROUP BY sex;
Copy after login

How to implement group sum in mysql

It can be seen that the genders of the mip_demo table are grouped, and the sum of the scores of each group is calculated.

Extended knowledge:

  • ##GROUP BYKeywords are used for grouping queries

The GROUP BY statement groups rows with the same value into summary rows, such as "Find the number of customers per region."

GROUP BY statement is usually used with aggregate functions (COUNT(), MAX(), MIN(),

SUM(), AVG()) to group the result set by one or more columns .

  • SUM() function

The SUM() function is used to calculate the sum of a set of values ​​or expressions. The syntax of the SUM() function As follows:

SUM(DISTINCT expression)
Copy after login
How does the SUM() function work?

  • If the SUM function is used in a SELECT statement that does not return 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.

[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to implement group sum 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!