开发者

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.

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?


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' 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, 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜