MySQL SUM function to add decimal values

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   Money DECIMAL(7,2)
);
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100.67);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(199.33);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(500);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(400);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(800);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Money  |
+--------+
| 100.67 |
| 199.33 |
| 500.00 |
| 400.00 |
| 800.00 |
+--------+
5 rows in set (0.00 sec)

Following is the query to add decimal values/ −

mysql> select sum(Money) from DemoTable;

This will produce the following output −

+------------+
| sum(Money) |
+------------+
| 2000.00   |
+------------+
1 row in set (0.00 sec)
raja
Published on 25-Sep-2019 12:09:55
Advertisements