MySQL大于或等于运算符忽略其或等于义务
如果一行中的价格为 38.03
,则以下搜索限制都应返回包含结果的行。
WHERE 价格 >= '38.02' AND 价格 <= '38.03'
(可行)
WHERE 价格 >= '20' AND 价格 <= '100'
(这有效)
WHERE 价格 >= '38.03' AND 价格 <= '38.03'
(这不起作用)
WHERE 价格 >= '38.03' AND 价格 <= '100'
(这不起作用)
WHERE 价格 >= '38.03'
(这不起作用)
WHERE 价格 <= '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'
(This works)
WHERE price >= '20' AND price <= '100'
(This works)
WHERE price >= '38.03' AND price <= '38.03'
(This doesn't work)
WHERE price >= '38.03' AND price <= '100'
(This doesn't work)
WHERE price >= '38.03'
(This doesn't work)
WHERE price <= '38.03'
(This works)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,就精度而言,
float
是一种有缺陷的数据类型。如果将12
表示为 float,您将得到11.99999999999998
或其他内容。'38.03'
可以转换为十进制,或者其他更精确的数据类型(取决于 RDBMS,我在这里是一般性的),并且它将与浮点值不同。float是32位的,精度低。 Double 工作得更好,因为它是 64 位数据类型。一些系统中的十进制数据类型是 128 位数字数据类型,用于存储非常精确的数值,通常用于货币计价。
并且,请避免使用
=
运算符比较float
值的习惯。浮点数用于近似和快速计算,只有与范围进行比较才能检查浮点数的值。这基本上对每个系统都有效。keep in mind that
float
is a flawed data type when it comes to precision. If you represent12
as float, you will get11.99999999999998
or something.'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.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.
And, skip the habit of comparing using the
=
operator, offloat
values. Floats are used for approximate and fast calculations, and only comparison with a range is acceptable for checking the value of afloat
. That's valid for basically every single system.当您使用引号 (') 时,您的变量将被视为字符串。我认为那是你的问题。
尝试:WHERE 价格 >= 38.03 AND 价格 <= 38.03
When you use quotes (') your variables will be treated as strings. I think thats your problem.
Try: WHERE price >= 38.03 AND price <= 38.03