3

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.

1
  • 2
    My first guess is that no rows matched the query in that case... Commented Oct 5, 2010 at 19:21

2 Answers 2

7

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' 
Sign up to request clarification or add additional context in comments.

1 Comment

I will mention that to him for further tasks, I'll accept your answer after I get back from work, I need to go!
4

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

1 Comment

Thanks you, that makes it easier to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.