- 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
How can a query multiply 2 cells for each row in MySQL?
You can use multiplication operator (*) between two cells. The syntax is as follows
SELECT yourColumnName1,yourColumnName2, yourColumnName1*yourColumnName2 as ‘anyVariableName’ from yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table MultiplicationDemo -> ( -> FirstPrice int, -> SecondPrice int -> ); Query OK, 0 rows affected (0.63 sec)
Now you can display all records from the table using insert command. The query is as follows
mysql> insert into MultiplicationDemo values(10,2); Query OK, 1 row affected (0.17 sec) mysql> insert into MultiplicationDemo values(4,2); Query OK, 1 row affected (0.30 sec) mysql> insert into MultiplicationDemo values(5,6); Query OK, 1 row affected (0.17 sec) mysql> insert into MultiplicationDemo values(6,3); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement. The query is as follows
mysql> select *from MultiplicationDemo;
The following is the output
+------------+-------------+ | FirstPrice | SecondPrice | +------------+-------------+ | 10 | 2 | | 4 | 2 | | 5 | 6 | | 6 | 3 | +------------+-------------+ 4 rows in set (0.00 sec)
Here is the query to multiply 2 cells
mysql> select FirstPrice,SecondPrice, -> FirstPrice*SecondPrice as 'MultiplicationResult' -> from MultiplicationDemo;
The following is the output
+------------+-------------+----------------------+ | FirstPrice | SecondPrice | MultiplicationResult | +------------+-------------+----------------------+ | 10 | 2 | 20 | | 4 | 2 | 8 | | 5 | 6 | 30 | | 6 | 3 | 18 | +------------+-------------+----------------------+ 4 rows in set (0.03 sec)
Advertisements