In mysql, you can use the aggregate function sum() to sum the elements in a column. The function of this function is to return the sum of the specified column. The syntax is "SELECT SUM (column name 1) AS column name 1,SUM(column name 2) AS column name 2 ... FROM table name;".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
Use the aggregate function sum() to sum the elements of a column in MySQL. The syntax is as follows -
select sum(yourColumnName1) as anyVariableName1,sum(yourColumnName2) as anyVariableName2,sum(yourColumnName3) as anyVariableName3,............N from yourTableName;
To understand the above syntax, let us create a table. Following is the query to create the table -
mysql> create table SumDemoOnColumns −> ( −> First int, −> Second int, −> Third int −> );
Use insert command to insert some data in the table. The query is as follows -
mysql> insert into SumDemoOnColumns values(10,20,30); mysql> insert into SumDemoOnColumns values(40,50,60); mysql> insert into SumDemoOnColumns values(70,80,90); mysql> insert into SumDemoOnColumns values(100,110,120);
Use the select statement to display all records in the table. The query is as follows -
mysql> select *from SumDemoOnColumns;
The following is the output-
The following is the query to get the column sum-
mysql> select sum(First) as First,sum(Second) as Second,sum(Third) as Third from SumDemoOnColumns;
The following is the output-
Recommended learning:mysql video tutorial
The above is the detailed content of How to implement column sum in mysql. For more information, please follow other related articles on the PHP Chinese website!