MySQL大于或等于运算符正在忽略其等于义务 [英] MySQL greater than or equal to operator is ignoring its or equal to obligation

查看:85
本文介绍了MySQL大于或等于运算符正在忽略其等于义务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果行中的价格为38.03,则以下搜索限制应全部返回包含结果的行.

If a price in a row is 38.03, then the following search restrictions should all return the row containg the result.

WHERE price >= '38.02' AND price <= '38.03'(有效)

WHERE price >= '20' AND price <= '100'(有效)

WHERE price >= '38.03' AND price <= '38.03'(这不起作用)

WHERE price >= '38.03' AND price <= '100'(这不起作用)

WHERE price >= '38.03'(这不起作用)

WHERE price <= '38.03'(有效)

价格以浮动价格存储在数据库中.

The price is stored as a float in the DB.

因此,基本上,<=是有效的,而>=是无效的.有什么可能的原因吗?

So basically, <= is working whereas >= is not. Is there a reason why that could be?

推荐答案

请牢记float在精度方面是有缺陷的数据类型.如果将12表示为浮点数,则会得到11.99999999999998或类似内容.

keep in mind that float is a flawed data type when it comes to precision. If you represent 12 as float, you will get 11.99999999999998 or something.

'38.03'可以转换为十进制或其他更精确的数据类型(取决于RDBMS,在这里我很一般),它将不同于浮点值.

'38.03' can be converted to decimal, or other data type that is more precise (depending on RDBMS, I am being general here), and it will differ from the float value.

浮点数是32位,精度低. Double是64位数据类型,效果更好.在某些系统中,十进制数据类型是128位数字数据类型,用于存储非常精确的数值,通常用于表示货币.

float is 32 bit, low precision. Double works a lot better, being 64 bit data type. Decimal data type in some systems are 128 bit numeric data types for storing very precise numeric values, and is usually used for denominating money.

并且,跳过使用=运算符比较float值的习惯.浮点数用于近似和快速计算,并且只有与范围的比较才可以用于检查float的值.基本上对每个系统都有效.

And, skip the habit of comparing using the = operator, of float values. Floats are used for approximate and fast calculations, and only comparison with a range is acceptable for checking the value of a float. That's valid for basically every single system.

这篇关于MySQL大于或等于运算符正在忽略其等于义务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆