子句之间的多个 MySql WHERE
MySql 新手程序员感谢您的耐心等待。
我试图跟踪满足 3 个不同条件的表中的 ID 号,这是我得到的结果,但是查询不会返回表中明确匹配的任何结果。想法?
SELECT *
FROM `table`
WHERE `x` BETWEEN 80 AND 20
AND `y` BETWEEN 120 AND 20
AND `z` BETWEEN 40 AND 10
LIMIT 0 , 30
从理论上讲,我认为这应该有效吗?
Newbie MySql programmer thanks for the patience.
Im trying to track an Id number in a table where 3 different conditions are met this is what Iv got however the query dosent return any results where there are clearly matches in the table. Thoughts?
SELECT *
FROM `table`
WHERE `x` BETWEEN 80 AND 20
AND `y` BETWEEN 120 AND 20
AND `z` BETWEEN 40 AND 10
LIMIT 0 , 30
Am I right in theory to think that this should work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接近,但没有雪茄。 :)
解释一下,SQL 服务器通常评估
x BETWEEN val1 AND val2
与x >= val1 AND x <= val2
相同。按照您原始查询的编写方式,第一个条件将是x >= 120 AND x <= 20)
,这显然不是您想要的。不同条件周围的括号可确保在考虑 AND 之前对每个条件进行完整评估。它在 SQL 中的大多数情况下都会产生影响,即使没有影响,使用它们也是一个好主意,这样 6 个月后当您(或其他人)必须再次查看查询时,您的意图就很明确。
Close, but no cigar. :)
To explain, SQL servers generally evaluate
x BETWEEN val1 AND val2
the same asx >= val1 AND x <= val2
. The way your original query was written, the first condition would bex >= 120 AND x <= 20)
, which obviously wasn't what you intended.The parentheses around the different conditions make sure that each is evaluated completely before the AND is considered. It makes a difference most of the time in SQL, and even when it doesn't it's a good idea to use them so your intentions are clear 6 months from now when you (or someone else) has to look at the query again.
我认为范围需要相反:
I think the range needs to be the other way around: