MySQL: multiple AND queries using BETWEEN?

I saw a colleague use this to get users from a table in these conditions:

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

He said that there is a problem (it does not display any lines when it was placed there AND activated='yes', but the MySQL error was not selected.

Can't you do it? Do you need to put it in brackets or something crazy to tie BETWEENand AND?

Dates are in the correct format, by the way.

+3
source share
2 answers

No, everything will be fine. However, you may need to format your request so that it is clear that it ANDis autonomous and refers to the expression BETWEEN .. AND ...:

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

. .

+4

Source: https://habr.com/ru/post/1768112/


All Articles