How can a query multiply 2 cells for each row in MySQL?

MySQLMySQLi Database

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)
raja
Published on 16-Jan-2019 12:52:26
Advertisements