MySQL:使用 BETWEEN 进行多个 AND 查询?

发布于 2024-09-26 09:05:54 字数 344 浏览 0 评论 0原文

我见过一位同事在这些条件下使用它从表中获取用户:

SELECT * FROM users WHERE gender ='male' 
AND activated='yes' 
AND date_registered BETWEEN '$date1' AND '$date2' 

他说存在问题(当 ANDactivated='yes' 也放在那里时,它不会输出任何行,但没有抛出 MySQL 错误。

您是否需要将其放在括号或其他疯狂的内容中才能将 BETWEENAND 关联起来

?顺便一提。

I had seen a colleague use this to fetch users from a table under these conditions:

SELECT * FROM users WHERE gender ='male' 
AND activated='yes' 
AND date_registered BETWEEN '$date1' AND '$date2' 

He said there was an problem (it not outputting any rows when the AND activated='yes' was put there as well, but no MySQL error was thrown.

Can you not do this? Do you need to put it in brackets or something crazy to associate the BETWEEN and AND?

Dates are in correct format by the way.

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

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

发布评论

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

评论(2

贱贱哒 2024-10-03 09:05:55

不需要括号。你已经有了正确的语法。

No brackets required. You've got the correct syntax.

一个人的旅程 2024-10-03 09:05:54

不,它会工作得很好。但是,您可能希望格式化查询,以便清楚哪个 AND 是独立的,哪个属于 BETWEEN .. AND ... 语句:

SELECT * FROM users 
WHERE gender ='male' 
   AND activated='yes' 
   AND date_registered BETWEEN '$date1' AND '$date2' 

Nope, it will work just fine. However, you might want to format your query so it is clear which AND is standalone, and which belongs to a BETWEEN .. AND ... statement:

SELECT * FROM users 
WHERE gender ='male' 
   AND activated='yes' 
   AND date_registered BETWEEN '$date1' AND '$date2' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文