Trying to display numbers with commas while fetching it from database

时间:2016-04-07 10:40:12

标签: mysql

I'm trying to display the numbers with commas while I'm fetching it from my mySQL database. The values which are available should be displayed as xx,xx,xxx and if the rows does not have any value, it should show 0.00. I'm trying with the below code:

SELECT
    `table1`.`user_name`,
    FORMAT(`table2`.`value`, 2) AS `value`,
    `table1`.`user_id`
FROM
    `table1`
LEFT OUTER JOIN `table2` ON `table2`.`user_id` = `table1`.`user_id`
LEFT OUTER JOIN `table3` ON `table3`.`user_id` = `table1`.`user_id`
LEFT OUTER JOIN `table4` ON `table4`.`role_id` = `table4`.`role_id`
WHERE
    `table4`.`role_id` = '5'

Now, the problem is, if the values are available I'm getting it properly, but if there is no value, it shows null. What should I do to show 0.00 instead of null?

1 个答案:

答案 0 :(得分:0)

You can use IFNULL() for that to shows desired output when the value is NULL:

SELECT
    `table1`.`user_name`,
    IFNULL(FORMAT(`table2`.`value`, 2),'0.00') AS `value`,
    `table1`.`user_id`
FROM
    `table1`
LEFT OUTER JOIN `table2` ON `table2`.`user_id` = `table1`.`user_id`
LEFT OUTER JOIN `table3` ON `table3`.`user_id` = `table1`.`user_id`
LEFT OUTER JOIN `table4` ON `table4`.`role_id` = `table4`.`role_id`
WHERE
    `table4`.`role_id` = '5'