MySQL MySQL大于或等于运算符忽略其或等于义务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3667161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL greater than or equal to operator is ignoring its or equal to obligation
提问by bcmcfc
If a price in a row is 38.03
, then the following search restrictions should all return the row containg the result.
如果一行中的价格是38.03
,则以下搜索限制都应返回包含结果的行。
WHERE price >= '38.02' AND price <= '38.03'
(This works)
WHERE price >= '38.02' AND price <= '38.03'
(这有效)
WHERE price >= '20' AND price <= '100'
(This works)
WHERE price >= '20' AND price <= '100'
(这有效)
WHERE price >= '38.03' AND price <= '38.03'
(This doesn't work)
WHERE price >= '38.03' AND price <= '38.03'
(这不起作用)
WHERE price >= '38.03' AND price <= '100'
(This doesn't work)
WHERE price >= '38.03' AND price <= '100'
(这不起作用)
WHERE price >= '38.03'
(This doesn't work)
WHERE price >= '38.03'
(这不起作用)
WHERE price <= '38.03'
(This works)
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?
所以基本上,<=
正在工作而>=
没有。有什么原因吗?
回答by AlexanderMP
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.
请记住,float
就精度而言,这是一种有缺陷的数据类型。如果你表示12
为浮动,你会得到11.99999999999998
什么。
'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.
'38.03'
可以转换为十进制,或者其他更精确的数据类型(取决于RDBMS,我这里是通用的),它会与浮点值不同。
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.
浮点数为 32 位,精度低。Double 效果更好,是 64 位数据类型。某些系统中的十进制数据类型是 128 位数字数据类型,用于存储非常精确的数值,通常用于货币计价。
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.
并且,跳过使用值的=
运算符进行比较的习惯float
。浮点数用于近似和快速计算,只有与范围比较才能检查 a 的值float
。这基本上适用于每个系统。
回答by Johan
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
尝试:WHERE price >= 38.03 AND price <= 38.03