子句之间的多个 MySql WHERE

发布于 2024-10-21 16:19:06 字数 260 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

美羊羊 2024-10-28 16:19:06

接近,但没有雪茄。 :)

SELECT * FROM table 
WHERE (x BETWEEN 20 AND 80) AND 
(y BETWEEN 20 AND 120) AND 
(z BETWEEN 10 AND 40) LIMIT 0 , 30

解释一下,SQL 服务器通常评估 x BETWEEN val1 AND val2x >= val1 AND x <= val2 相同。按照您原始查询的编写方式,第一个条件将是 x >= 120 AND x <= 20),这显然不是您想要的。

不同条件周围的括号可确保在考虑 AND 之前对每个条件进行完整评估。它在 SQL 中的大多数情况下都会产生影响,即使没有影响,使用它们也是一个好主意,这样 6 个月后当您(或其他人)必须再次查看查询时,您的意图就很明确。

Close, but no cigar. :)

SELECT * FROM table 
WHERE (x BETWEEN 20 AND 80) AND 
(y BETWEEN 20 AND 120) AND 
(z BETWEEN 10 AND 40) LIMIT 0 , 30

To explain, SQL servers generally evaluate x BETWEEN val1 AND val2 the same as x >= val1 AND x <= val2. The way your original query was written, the first condition would be x >= 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.

场罚期间 2024-10-28 16:19:06
SELECT * FROM table WHERE 
      (x BETWEEN 20 AND 80) AND 
      (y BETWEEN 20 AND 120) AND 
      (z BETWEEN 10 AND 40) 
LIMIT 0 , 30
SELECT * FROM table WHERE 
      (x BETWEEN 20 AND 80) AND 
      (y BETWEEN 20 AND 120) AND 
      (z BETWEEN 10 AND 40) 
LIMIT 0 , 30
飞烟轻若梦 2024-10-28 16:19:06

我认为范围需要相反:

SELECT * FROM table WHERE x BETWEEN 20 AND 80 AND y BETWEEN 20 AND 120 AND z BETWEEN 10 AND 40 LIMIT 0 , 30

I think the range needs to be the other way around:

SELECT * FROM table WHERE x BETWEEN 20 AND 80 AND y BETWEEN 20 AND 120 AND z BETWEEN 10 AND 40 LIMIT 0 , 30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文