[Very important] Python-SQLAlchemy the mysql database queries Notes condition of fractional type decimal [summary]

Floating-point additions and deletions to change search on the database is a more difficult problem, to sum up here

 

Differences in MySQL and Float Double Decimal type, etc.

Presence float, double non-standard data types like MySQL, there are decimal this standard data types.

The difference is that, float, double and other non-standard type, stored in the DB are approximations, the numerical Decimal places stored as a string.
float, double type can store floating-point (ie decimal type), but there is a downside float, when you are given data is an integer, then you deal with it as an integer. So we naturally encounter problems when accessing monetary value, my default values are: 0.00 actual storage is 0, I access the same currency as 12.00, the actual storage is 12.
Fortunately, mysql provides two data types: decimal, this data type can easily solve the above problem: decimal type is MySQL to achieve the same type, which is allowed in the SQL92 standard. They used to hold values are important requirements for accurate precision, such as data related to money.

 

For this reason: money in operations and associated floating type we use decimal decimal type, additions and deletions to the time, is no problem! But there is a detail in the query time is very important, otherwise it will lead to problems because of decimal precision query can not obtain the results you want [particular query when conditions are equal to]

For example, this table above, the accuracy of our database storage is nine or six decimal places, and if we use the query conditions: float numbers when the result is no problem , but if you use decimal (0.05) decimal data type of query can be not want conceivable query results

这里使用float类型的数字做查询条件
buy_order_model.price == 0.05

这里使用的是decimal(0.05)类型做查询条件的条件表达式
buy_order_model.price == 0.05000000000000000277555756156289135105907917022705078125

 

mysql summary: decimal decimal type for CRUD, but do not do the query, be sure to first decimal is converted to float before the query expression in the formation conditions!

 

 

Guess you like

Origin blog.csdn.net/weixin_43343144/article/details/91418343