Note for processing NULL values in Mysql integer operations: mysqlnull

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.