For this, you can use CONCAT() and round(). Let us first create a table −
mysql> create table DemoTable1844 ( Number int, TotalNumber int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1844 values(50,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(80,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(98,500); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1844 values(45,500); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1844;
This will produce the following output −
+--------+-------------+ | Number | TotalNumber | +--------+-------------+ | 50 | 500 | | 80 | 500 | | 98 | 500 | | 45 | 500 | +--------+-------------+ 4 rows in set (0.00 sec)
Here is the query to calculating percentage in a query and rounding off the result −
mysql> select concat(round(((Number / TotalNumber) * 100 ),2), '%') as Result from DemoTable1844;
This will produce the following output −
+--------+ | Result | +--------+ | 10.00% | | 16.00% | | 19.60% | | 9.00% | +--------+ 4 rows in set (0.00 sec)