
- 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
Select and add result of multiplying two columns from a table in MySQL?
You can use aggregate function SUM() for this. Let us first create a table −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerProductName varchar(100), CustomerProductQuantity int, CustomerPrice int ); Query OK, 0 rows affected (0.17 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',5,400); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-2',3,100); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',2,300); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-1',5,50); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-3',6,10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(CustomerProductName,CustomerProductQuantity,CustomerPrice) values('Product-2',10,20); Query OK, 1 row affected (0.03 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+---------------------+-------------------------+---------------+ | CustomerId | CustomerProductName | CustomerProductQuantity | CustomerPrice | +------------+---------------------+-------------------------+---------------+ | 1 | Product-1 | 5 | 400 | | 2 | Product-2 | 3 | 100 | | 3 | Product-1 | 2 | 300 | | 4 | Product-1 | 5 | 50 | | 5 | Product-3 | 6 | 10 | | 6 | Product-2 | 10 | 20 | +------------+---------------------+-------------------------+---------------+ 6 rows in set (0.00 sec)
Following is the query to select and add result of multiplying two columns (CustomerProductQuantity*CustomerPrice) from a table in MySQL.
mysql> select CustomerProductName, SUM(CustomerProductQuantity*CustomerPrice) AS TOTAL_PRICE from DemoTable group by CustomerProductName;
This will produce the following output −
+---------------------+-------------+ | CustomerProductName | TOTAL_PRICE | +---------------------+-------------+ | Product-1 | 2850 | | Product-2 | 500 | | Product-3 | 60 | +---------------------+-------------+ 3 rows in set (0.00 sec)
- Related Articles
- Select distinct names from two columns in MySQL and display the result in a single column
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- MYSQL select DISTINCT values from two columns?
- Divide numbers from two columns and display result in a new column with MySQL
- SELECT not null column from two columns in MySQL?
- Add new MySQL table columns and create indexes?
- Select maximum of sum of two columns in MySQL
- Add a new column to table and fill it with the data of two other columns of the same table in MySQL?
- Order a MySQL table by two columns?
- Split float value in two columns of a MySQL table?
- Select rows from a MySQL table and display using IN()
- How to select first and last data row from a MySQL result?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- MySQL query to multiply values of two rows and add the result

Advertisements