dongshai8330
2010-10-05 19:16 阅读 405
已采纳

MySQL:使用BETWEEN的多个AND查询?

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanheye7909 duanheye7909 2010-10-05 19:20

    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' 
    
    点赞 评论 复制链接分享
  • duanqiu9104 duanqiu9104 2010-10-05 19:19

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

    点赞 评论 复制链接分享

相关推荐