The float type of mysql is very strange. the two values are not equal.

Source: Internet
Author: User
The float type of mysql is very strange. The two tables with different values have a lot of data, but there are several very strange ones. The fields are of the float type and the two values are the same.

Payment Value = 299.7
Feiyong value = 299.7

Query statement: feiyong> 0 and Payment> 0 and Payment
In this case, the query is still available, and only a few items have this problem, and others are normal.


Reply to discussion (solution)

The floating point number indicates a large number, and some precision is lost.
Therefore, when compared, the absolute values of two numbers are considered equal when the difference is smaller than a certain decimal number.
Or use the round to obtain the valid number before comparison.
If your application requires precise DECIMAL places (such as finance), use the DECIMAL type.

This is quite normal. after all, there is no way for float data to ensure data accuracy.

The floating point number indicates a large number, and some precision is lost.
Therefore, when compared, the absolute values of two numbers are considered equal when the difference is smaller than a certain decimal number.
Or use the round to obtain the valid number before comparison.
If your application requires precise DECIMAL places (such as finance), use the DECIMAL type.



Now, how can I change the query statement to make the query normal? thank you.

We recommend that you change the data type to DECIMAL.

select ..., abs(Payment-feiyong) as aform ......where feiyong>0 and Payment>0having a > 1e-5

select ..., (Payment-feiyong) as aform ......where feiyong>0 and Payment>0having a > 1e-5

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.