Mysql If Else

I need hlp with a mysql query. It would be like the following. (I can already get records that are 30 days old )

[php]SELECT *

IF reminder_date IS NULL date_sub(now(), INTERVAL 30 DAY) >= my_date_column

ELSE IF reminder_date IS NOT NULL AND date_sub(now(), INTERVAL 30 DAY) >= my_date_column AND NOW() >=reminder_date

FROM mytable

PSUEDO CODE

If reminder_date is NOT set AND today is greater than or equal to my_date column, Select all records from my table that are 30 days or older than my_date column.

If reminder_date is set and today is greater than or equal to reminder_date, Select all records from my table that are 30 days or older than my_date column.[/php]

So, you pull the same thing based on two conditions. So, you do not need to use the ELSE clause.
Just use the standard WHERE clause and use the form of (condition1) OR (condition2)…

Something like this should work, but, it is untested as I have not data to test it with…
[php]
$query=“SELECT * FROM mytable WHERE
(reminder_date IS NULL AND date_sub(now(), INTERVAL 30 DAY) >= my_date_column)
OR
(reminder_date IS NOT NULL AND date_sub(now(), INTERVAL 30 DAY) >= my_date_column AND NOW() >=reminder_date)”;
[/php]
In your current IF version, you are missing an AND in the first condition clause. Hope this works…

[member=43746]ErnieAlex[/member] , Nice Job. Your code is the right answer.

Glad I could help…

Sponsor our Newsletter | Privacy Policy | Terms of Service