Home  >  Article  >  Database  >  How to implement column sum in mysql

How to implement column sum in mysql

WBOY
WBOYOriginal
2022-05-27 16:59:587788browse

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;".

How to implement column sum in mysql

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

How to implement column summation in mysql

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-

How to implement column sum in mysql

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-

How to implement column sum in mysql

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!

Statement:
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