Note for processing NULL values in Mysql integer operations: mysqlnull
When CleverCode was recently exporting a report, it was found that the integer was null after the integer was subtracted. This is a matter of detail. I hope you will pay attention to it later.
Total and used values in Table 1 are all integer values, which can be null.
2. Incorrect operation statement Subtraction
select id,total,used,(total - used) as have from test_table
3. Use ifnull to handle null.
select id,ifnull(total,0) as total,ifnull(used,0) as used,(ifnull(total,0) - ifnull(used,0)) as have from test_table
4. Suggestions
1) when designing a table, if you find that this column is the column to be computed, we recommend that you set the default value, such as 0; 2) when performing an integer column operation, whether it is adding or subtracting, multiplication, division, etc. You must use ifnull to process columns. Otherwise, the report data is incorrect.
These are all minor details. You can understand them at a Glance. I hope you will pay attention to the details. Otherwise, a financial report calculation error will cause serious consequences!
Copyright statement: 1) original works are from the "CleverCode blog". Do not reprint them. Otherwise, the copyright is held legally liable.
2) original address: http://blog.csdn.net/clevercode/article/details/47035975.
3) classification address (Mysql database summary): http://blog.csdn.net/clevercode/article/category/3262205 (blog continues to increase, follow the favorite)
4) Welcome to my blog for more highlights: http://blog.csdn.net/clevercode.
4. Suggestions
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.