- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Combine SUM and FORMAT in MySQL to format the result
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)
Advertisements