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;".
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 需要进行分组的字段名;
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;
Find the sum of the scores of boys and girls in groups.
SELECT sex,SUM(score) FROM mip_demo GROUP BY sex;
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
SUM(), AVG()) to group the result set by one or more columns .
SUM(DISTINCT expression)
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!