为什么在运算符中无法正常工作,在MySQL查询中具有浮点值?

发布于 2025-01-18 12:04:18 字数 1423 浏览 4 评论 0原文

我正在使用 MySQL 查询。在工作时,我遇到了包含 IN 运算符的查询问题。当 IN 与整数值一起使用时,它可以正常工作。但是,当 IN 与浮点值一起使用时,不会出现正确的输出。如果 WHERE 子句用于使用 IN 运算符检查两个值,则在数据类型为 float 的值的情况下,它仅在少数情况下检查并返回查询中给定值中第一个出现在表中的值。 为什么会出现这种情况以及如何解决?

假设有一个表 customer ,其属性 customer_id int(4)


customer_id
3001
3003
3007
3008
3009
3012

假设有一个查询:

select customer_id from customer where customer_id in (3007,3008,3009);

上述查询的输出: 在这里它工作正常并输出包含指定属性下的这些值的记录

但是当存在 float 数据类型的值时会出现问题

有一个带有属性 purch_amt float(6,2)

purch_amt
110.5
948.5
的表 orders 1983.43

如果查询是:

select purch_amt from orders where purch_amt in(948.5,110.5);

上述查询的输出: 它返回本例表中的记录。

如果查询是:

select purch_amt from orders where purch_amt in(948.5,1983.43);

上述查询的输出: 对于这种情况,它仅返回包含表中较早出现的值的记录。 为什么会发生这种情况以及我们如何解决这个问题?

I was working with MySQL Queries. While working I am having problem with the queries containing IN operator. When IN is used with integer values it works properly. But when IN is used with floating-point values proper output is not coming. If WHERE clause is used to check two values with IN operator, in case of values with datatype float it only checks and returns the value which appears first in the table from the given values in query for few cases.
Why this occurs and how to solve this?

Let there is a table customer with attribute customer_id int(4)


customer_id
3001
3003
3007
3008
3009
3012

Lets say there is query:

select customer_id from customer where customer_id in (3007,3008,3009);

Output for above query:
Here it works fine and outputs the records containing these values under the specified attribute

But when there are values of float datatype a problem occurs

There is a table orders with attribute purch_amt float(6,2)

purch_amt
110.5
948.5
1983.43

If the query is:

select purch_amt from orders where purch_amt in(948.5,110.5);

Output for above query:
It returns the records from the table for this case.

If the query is:

select purch_amt from orders where purch_amt in(948.5,1983.43);

Output for above query:
It only returns the record containing the value which is present earlier in the table for this case.
Why is this occuring and how can we solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文