Combine SUM and FORMAT in MySQL to format the result

MySQLMySQLi Database

Let us create a table −

mysql> create table DemoTable1950
   (
   Amount float
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1950 values(45.60);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1950 values(101.78);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1950 values(75.90);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1950 values(89.45);
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1950;

This will produce the following output −

+--------+
| Amount |
+--------+
|   45.6 |
| 101.78 |
|   75.9 |
|  89.45 |
+--------+
4 rows in set (0.00 sec)

Following is the query to combine SUM and FORMAT in MySQL:

mysql> select format(sum(Amount),2) as TotalSum from DemoTable1950;

This will produce the following output −

+----------+
| TotalSum |
+----------+
| 312.73   |
+----------+
1 row in set (0.00 sec)
raja
Published on 31-Dec-2019 06:48:04
Advertisements