Query to divide the values of two columns and display the result in a new column using MySQL wildcard?


Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Value1 int,
   Value2 int
);
Query OK, 0 rows affected (0.55 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Value1,Value2) values(100,150);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Value1,Value2) values(500,1000);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable(Value1,Value2) values(15000,18000);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+--------+--------+
| Id | Value1 | Value2 |
+----+--------+--------+
|  1 | 100    |    150 |
|  2 | 500    |   1000 |
|  3 | 15000  |  18000 |
+----+--------+--------+
3 rows in set (0.00 sec)

Let us now divide the values from two columns and display the result in a new column with MySQL wildcards −

mysql> select *,(Value1/Value2) AS Result from DemoTable;

This will produce the following output −

+----+--------+--------+--------+
| Id | Value1 | Value2 | Result |
+----+--------+--------+--------+
|  1 |    100 |    150 | 0.6667 |
|  2 |    500 |   1000 | 0.5000 |
|  3 |  15000 |  18000 | 0.8333 |
+----+--------+--------+--------+
3 rows in set (0.00 sec)

Updated on: 25-Sep-2019

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements