tags:

views:

35

answers:

2

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.

+4  A: 

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

p.campbell
Thanks you, that makes it easier to explain.
John
+4  A: 

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' 
Wrikken
I will mention that to him for further tasks, I'll accept your answer after I get back from work, I need to go!
John