MySQL IF() statement is an usual if else statement we used, just that the syntax is different, we shall conform the MySQL standard to write a MySQL if statement. Its used when we wanna check if condition in our sql statement. Example below:
1.$sql="SELECT * FROM users WHERE DATEDIFF(CURDATE(),IF('register_date'=NULL,CURDATE(),'register_date'))='1'";MySQL IF() statement holds 3 expressions, IF(expr1, expr2, expr3). expr1 used to check condition, if expr1 TRUE, then return expr2, else return expr3. The example above interpreted as get all the users records who register yesterday, where difference between today and register date equals to 1 day. When it performs CURDATE() function, it will checks if register date is NULL, if its NULL then return CURDATE(), else return register date. So, for those register date equals to NULL, it will return today, CURDATE() and hence results in return empty result or force the sql statement not be executed, as DATEDIFF(CURDATE(),CURDATE())<>1.



