
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to calculate value from multiple columns in MySQL?
To calculate a value from multiple columns, use GROUP BY. Following is the syntax −
select yourColumnName1,sum(yourColumnName2*yourColumnName3) AS anyAliasName from yourTableName group by yourColumnName1;
Let us first create a table −
mysql> create table calculateValueDemo -> ( -> Id int, -> ProductPrice int, -> ProductWeight int -> ); Query OK, 0 rows affected (0.56 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into calculateValueDemo values(100,35,5); Query OK, 1 row affected (0.16 sec) mysql> insert into calculateValueDemo values(101,50,3); Query OK, 1 row affected (0.16 sec) mysql> insert into calculateValueDemo values(100,100,4); Query OK, 1 row affected (0.17 sec) mysql> insert into calculateValueDemo values(101,500,2); Query OK, 1 row affected (0.22 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from calculateValueDemo;
This will produce the following output −
+------+--------------+---------------+ | Id | ProductPrice | ProductWeight | +------+--------------+---------------+ | 100 | 35 | 5 | | 101 | 50 | 3 | | 100 | 100 | 4 | | 101 | 500 | 2 | +------+--------------+---------------+ 4 rows in set (0.00 sec)
Here is the query to calculate the value from multiple columns −
mysql> select Id,sum(ProductPrice*ProductWeight) AS Total from calculateValueDemo group by Id;
This will produce the following output −
+------+-------+ | Id | Total | +------+-------+ | 100 | 575 | | 101 | 1150 | +------+-------+ 2 rows in set (0.00 sec)
- Related Articles
- How to check multiple columns for a single value in MySQL?
- Get the minimum value from a list with multiple columns in MySQL?
- Count value for multiple columns in MySQL?\n
- How to search multiple columns in MySQL?
- How to order MySQL rows by multiple columns?
- How to order by the highest value from two columns in MySQL?
- MySQL multiple COUNT with multiple columns?
- How to use MySQL DISTINCT clause on multiple columns?
- How to alter multiple columns in a single statement in MySQL?
- How to check for duplicates in MySQL table over multiple columns?
- How to create conditions in a MySQL table with multiple columns?
- MySQL query to GROUP BY multiple columns
- How to calculate the difference between time in different MySQL columns?
- MySQL filtering by multiple columns?
- Get MySQL maximum value from 3 different columns?

Advertisements