Format MySQL records (price values) after multiplying them


To format records, use FORMAT(). Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Price decimal(10,4),
   -> Rate decimal(10,4)
   -> );
Query OK, 0 rows affected (0.96 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1000,10.2);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(2000,20.4);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(100,5);
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+---------+
| Price     | Rate    |
+-----------+---------+
| 1000.0000 | 10.2000 |
| 2000.0000 | 20.4000 |
|  100.0000 | 5.0000  |
+-----------+---------+
3 rows in set (0.00 sec)

Following is the query to format records −

mysql> select format((Price * Rate),2) from DemoTable;

This will produce the following output −

+--------------------------+
| format((Price * Rate),2) |
+--------------------------+
| 10,200.00                |
| 40,800.00                |
| 500.00                   |
+--------------------------+
3 rows in set (0.00 sec)

Updated on: 17-Dec-2019

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements