简体   繁体   English

大于或等于运算符的MySQL忽略了它或等于义务

[英]MySQL greater than or equal to operator is ignoring its or equal to obligation

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. 价格作为浮动存储在DB中。

So basically, <= is working whereas >= is not. 所以基本上, <=正在工作,而>=不是。 Is there a reason why that could be? 这有什么原因可以吗?

keep in mind that float is a flawed data type when it comes to precision. 请记住, floatfloat是一种有缺陷的数据类型。 If you represent 12 as float, you will get 11.99999999999998 or something. 如果您将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. float是32位,精度低。 Double works a lot better, being 64 bit data type. Double工作得更好,是64位数据类型。 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. 某些系统中的十进制数据类型是128位数字数据类型,用于存储非常精确的数值,通常用于计价货币。

And, skip the habit of comparing using the = operator, of float values. 并且,跳过使用=运算符比较float值的习惯。 Floats are used for approximate and fast calculations, and only comparison with a range is acceptable for checking the value of a float . float用于近似和快速计算,只有与范围的比较才能用于检查float的值。 That's valid for basically every single system. 这基本上对每个系统都有效。

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价格> = 38.03和价格<= 38.03

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM