我的发言如下:
SELECT * FROM users WHERE division >= '0' AND division <= '10' AND voice = '0' ORDER BY username DESC LIMIT 0, 10
这不会选择除法等于2的用户,但是
SELECT * FROM users WHERE division >= '1' AND division <= '9' AND voice = '0' ORDER BY username DESC LIMIT 0, 10
将选择用户
这是为什么?感谢您提供任何帮助。
答案 0 :(得分:3)
您似乎将division
存储为字符串。您可以尝试进行转换并与数字进行比较;
SELECT *
FROM users
WHERE division+0 between 0 and 10 AND voice = '0'
ORDER BY username DESC
LIMIT 0, 10 ;
这种方法的唯一问题是非数字值(比如'A11'
)也会匹配,因为MySQL会在添加中将这样的值视为“0”。
顺便说一句,对字符串的整数比较确实有效,但我不建议使用它们。考虑一下:
select (2 <= '10') as IntToString, ('2' <= '10') as StringToString
返回:
1 0
答案 1 :(得分:0)
如果除以数字,请使用
SELECT * FROM users WHERE division >= 0 AND division <= 10 AND voice = '0' ORDER BY username DESC LIMIT 0, 10
否则,请先使用Cast Functions将分部转换为数字。