MySQL 查询未显示正确的数据

发布于 2024-10-02 23:41:58 字数 406 浏览 0 评论 0原文

大家好,我有一个 MySQL 查询,

SELECT * FROM `product` WHERE `price` BETWEEN '60' AND '999' ORDER BY `product` DESC LIMIT 12 OFFSET 0

这显然选择了 12 个价格在 60 到 999 之间的产品。这正确地显示了行。

但是,如果我像这样的查询增加限制,

SELECT * FROM `product` WHERE `price` BETWEEN '60' AND '1000' ORDER BY `product` DESC LIMIT 12 OFFSET 0

则不会显示任何行。有什么想法为什么会发生这种情况吗? “价格”字段是浮点型字段

Hay Everyone, i have a MySQL query

SELECT * FROM `product` WHERE `price` BETWEEN '60' AND '999' ORDER BY `product` DESC LIMIT 12 OFFSET 0

This obviously select 12 products where the price is between 60 and 999. This shows the rows correctly.

However if i increase the limit like this query

SELECT * FROM `product` WHERE `price` BETWEEN '60' AND '1000' ORDER BY `product` DESC LIMIT 12 OFFSET 0

No rows get shown. Any ideas why this happens? The 'price' field is float type field

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

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

发布评论

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

评论(3

£噩梦荏苒 2024-10-09 23:41:58

删除 ' 因为字段类型是数字,而不是字符。

Remove the ' because the field types are numbers, not chars.

(り薆情海 2024-10-09 23:41:58

您将 '60''1000' 视为字符。使用数字:

where `price` between 60 and 1000

如果您使用 varchar 字符串,则没有行可以匹配 Between'60' 和 '1000'条件,因为'在 varchar 中,1000'小于'60'`。

它与 '999' 一起使用,因为可能有几个价格介于 '6''9' 之间。

mysql> select '60' < '9';
+------------+
| '60' < '9' |
+------------+
|          1 | 
+------------+
1 row in set (0.00 sec)

You are treating '60' and '1000' as characters. Use numbers:

where `price` between 60 and 1000

If you use varchar strings, there is no row that will match a between'60' and '1000'condition, since'1000'is smaller than'60'` in varchars.

It was working with '999' because there are probably a couple of prices ranged between '6' and '9'.

mysql> select '60' < '9';
+------------+
| '60' < '9' |
+------------+
|          1 | 
+------------+
1 row in set (0.00 sec)
赏烟花じ飞满天 2024-10-09 23:41:58

60、999 和 1000 被视为字符而不是 float 或 int,

它与 999 一起使用的原因是因为比较发生在 ascii 字符上。

对于 1000,您不会得到任何价格以 ASCII 开头在 6 到 1 之间的行,因为 6 的值高于 1。

The 60, 999 and 1000 are getting treated as chars instead of float or int,

The reason it works with 999 is because the comparision happens on ascii chars.

For 1000, you wont get any rows which will have price with ascii starting between 6 and 1 since 6 has higher value than 1.

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