- 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
How can I calculate the total value of products from my MySQL product table?
Let us first create a table −
mysql> create table DemoTable ( ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductQuantity int, ProductPrice int ); Query OK, 0 rows affected (0.19 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(10,100); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(5,11); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(3,140); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(ProductQuantity,ProductPrice) values(2,450); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+-----------------+--------------+ | ProductId | ProductQuantity | ProductPrice | +-----------+-----------------+--------------+ | 1 | 10 | 100 | | 2 | 5 | 11 | | 3 | 3 | 140 | | 4 | 2 | 450 | +-----------+-----------------+--------------+ 4 rows in set (0.00 sec)
Following is the query to calculate total value of products −
mysql> select sum(ProductQuantity*ProductPrice) as Total_of_Products from DemoTable;
This will produce the following output −
+-------------------+ | Total_of_Products | +-------------------+ | 2375 | +-------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to can I get the names of my MySQL table columns?
- How can I set my auto-increment value to begin from 1 in MySQL?
- How can I remove dandruff from my hair?
- How can I change the value of an instance of a row in MySQL table?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- How can I remove Python from my Windows Machine?
- How can I change the name of an existing column from a MySQL table?
- How can I drop an existing column from a MySQL table?
- MySQL SELECT products WHERE 'average price per product' < value?
- How can we extract a substring from the value of a column in MySQL table?
- Display all products having the highest amount in a MySQL table with product details?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- How can I delete MySQL temporary table?
- How can I return the values of columns from MySQL table as a set of values?
- How can I set the default value of my Tkinter Scale widget slider to 100?

Advertisements