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)
价格> = '38 .02'和价格<= '38 .03'(此作品)
WHERE price >= '20' AND price <= '100'
(This works)
价格> ='20'且价格<='100'(此作品)
WHERE price >= '38.03' AND price <= '38.03'
(This doesn't work)
价格> = '38 .03'和价格<= '38 .03'(这不起作用)
WHERE price >= '38.03' AND price <= '100'
(This doesn't work)
价格> = '38 .03'和价格<='100'(这不起作用)
WHERE price >= '38.03'
(This doesn't work)
价格> = '38 .03'(这不起作用)
WHERE price <= '38.03'
(This works)
价格<= '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?
所以基本上,<=正在工作,而> =不是。这有什么原因可以吗?
2 个解决方案
#1
24
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.
请记住,浮点数在精度方面是一种有缺陷的数据类型。如果你将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.
float是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.
并且,跳过使用=运算符比较浮点值的习惯。浮点数用于近似和快速计算,只有与范围进行比较才能检查浮点数的值。这基本上对每个系统都有效。
#2
0
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
#1
24
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.
请记住,浮点数在精度方面是一种有缺陷的数据类型。如果你将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.
float是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.
并且,跳过使用=运算符比较浮点值的习惯。浮点数用于近似和快速计算,只有与范围进行比较才能检查浮点数的值。这基本上对每个系统都有效。
#2
0
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