When you are working on MySQL, you should know how to sum multiple columns in MySQL. This is useful when you are trying to handle big datasets and are trying to take the sum of multiple columns in your dataset.
You need to have some idea about how the sum function works in a column in MySQL as this will help you understand how sum of multiple columns is taken.
The article below will discuss in detail that how to sum multiple columns in MySQL and will help you understand how the the sum function works in MySQL and how it will help in the sum of multiple objects.
How to Sum Multiple Columns in MySQL?
To sum multiple columns in MySQL, use the code SELECT sum(Column_1 + Column_2 + Column_3) as sum FROM table_example where Column_1, Column_2, and Column_3 are the columns that hold numeric data, for which we want to take the sum of data.
The above mentioned code will output the sum of those data in the form of a table which we would then will be able to see.
To know “How to sum multiple columns in MySQL”, you can follow the steps mentioned below:
First let’s discuss about how the code will look when you try to take the sum of multiple columns.
Once the code is clear we will take an example to make sure that you know how to properly implement the code
The code for taking the sum of multiple columns looks something like below mentioned:
SELECT sum(Column_1 + Column_2 + Column_3 ) as sum
FROM table_ncexample
The Column_1, Column_2, and Column_3 are the columns that hold numeric data, and we wish to take the sum of those data.
The above mentioned code will output the sum of those data in the form of a table which we would then will be able to see.
The sum part at the end of the code in the first line makes sure that the name of the output column will be sum.
Given below is an example of how this code will be implemented in an example table named table_ncexample:
ID | Name | Category | Total |
---|---|---|---|
1 | Sun | one | 115 |
2 | Mercury | two | 133 |
3 | Venus | Three | 234 |
4 | Earth | Four | 156 |
5 | Mars | five | 147 |
6 | Jupiter | six | 200 |
7 | Saturn | seven | 122 |
8 | Uranus | one | 782 |
9 | Neptune | Six | 292 |
10 | Galaxy | two | 289 |
The code for the sumation of the numerical data columns is given below:
SELECT sum(Sun + Mercury + Venus) as total
FROM table_ncexample
The output will be something like the below table:
Total |
482 |
Conclusion
To sum multiple columns in MySQL first you will have to use the code SELECT sum(Column_1 + Column_2 + Column_3 ) as sum FROM table_example
.The Column_1, Column_2, and Column_3 are the columns that hold numeric data, and we wish to take the sum of those data.
The above mentioned code will output the sum of those data in the form of a table which we would then will be able to see.