- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to multiply values of two rows and add the result
For this, use aggregate function SUM(). Within this method, multiply the row values. Let us first create a table −
mysql> create table DemoTable( ProductQuantity int, ProductPrice int ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,9); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(6,20); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(7,100); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------------+--------------+ | ProductQuantity | ProductPrice | +-----------------+--------------+ | 10 | 9 | | 6 | 20 | | 7 | 100 | +-----------------+--------------+ 3 rows in set (0.00 sec)
Following is the query to multiply values of two rows and add the result −
mysql> select SUM(ProductQuantity*ProductPrice) AS Total_Value from DemoTable;
This will produce the following output −
+-------------+ | Total_Value | +-------------+ | 910 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to sum up values of rows and sort the result?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL query to get the count of rows in which two or more specified values appear?
- MySQL query to compare and display only the rows with NULL values
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to count the duplicate ID values and display the result in a separate column
- Setting column values as column names in the MySQL query result?
- Select and add result of multiplying two columns from a table in MySQL?
- Multiply values of two columns and display it a new column in MySQL?
- In MySQL, how can we randomize set of rows or values in the result set?
- Program to multiply two strings and return result as string in C++
- MySQL query to find the number of rows in the last query
- MySQL query to match any of the two strings from column values
- Set the result of a query to a variable in MySQL?
- Shifting values of rows in MySQL to change the existing id values for existing rows?

Advertisements