
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiply Values of Two Rows and Add the Result in MySQL
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)
Advertisements