- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 calculate the total amount from column values with Cost and Quantity?
Let us first create a table −
mysql> create table DemoTable ( Cost int, Quantity int ); Query OK, 0 rows affected (0.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(65,2); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(290,4); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(40,3); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+----------+ | Cost | Quantity | +------+----------+ | 65 | 2 | | 290 | 4 | | 40 | 3 | +------+----------+ 3 rows in set (0.00 sec)
Following is the query to calculate the total amount −
mysql> select Cost,Quantity,Cost*Quantity AS Total_Amount from DemoTable;
This will produce the following output −
+------+----------+--------------+ | Cost | Quantity | Total_Amount | +------+----------+--------------+ | 65 | 2 | 130 | | 290 | 4 | 1160 | | 40 | 3 | 120 | +------+----------+--------------+ 3 rows in set (0.02 sec)
Advertisements
To Continue Learning Please Login
Login with Google