MySQL大于或等于运算符忽略其或等于义务

发布于 2024-09-17 20:09:05 字数 540 浏览 4 评论 0原文

如果一行中的价格为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-09-24 20:09:05

请记住,就精度而言,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 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.

醉生梦死 2024-09-24 20:09:05

当您使用引号 (') 时,您的变量将被视为字符串。我认为那是你的问题。

尝试: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文