List:General Discussion« Previous MessageNext Message »
From:SGreen Date:December 1 2004 8:00pm
Subject:Re: If statement in a where query....
View as plain text  
You don't need an IF only parentheses. This is almost a literal 
translation of your exact statement

Select * 
from db 
where (last_renewal_date between '2004-11-01' and '2004-11-30')
 or (last_renweal_date is null
        AND signup_date between '2004-11-01' and '2004-11-30'
    ) 
order by last_renewal_date

Since you are checking the same beginning and ending dates twice, it makes 
sense to move those values to their own variables (keeping the variable 
names in line with your example):

SET a='2004-11-01', b='2004-11-30';

Select * 
from db 
where (last_renewal_date between a and b)
 or (last_renewal_date is null
        AND signup_date between a and b
    ) 
order by last_renewal_date;

Shawn Green
Database Administrator
Unimin Corporation - Spruce Pine

Mike Morton <mike@stripped> wrote on 12/01/2004 02:47:19 PM:

> I am trying to find a list of people based on renewal dates, the logic 
of
> the query would be:
> 
> Select everything from the db where the last renewal date is between a 
and
> b, or if there is no last renewal date then where the signup date is 
between
> a and b
> 
> The query that I have is something like:
> 
> Select * 
> from db 
> where last_renewal_date
>     between '2004-11-01' and '2004-11-30' or
>     if(
>         length(last_renewal_date) < 1,
>         signup_date between '2004-11-01' and '2004-11-30'
>     ) 
> order by last_renewal_date
> 
> Obviously the syntax is wrong here, but is there a way to accomplish 
this
> logic using an if statement in the where statement?
> 
> TIA!
> 
> --
> Cheers
> 
> Mike Morton
> 
> ****************************************************
> *
> * Tel: 905-465-1263
> * Email: mike@stripped
> *
> ****************************************************
> 
> "Indeed, it would not be an exaggeration to describe the history of the
> computer industry for the past decade as a massive effort to keep up 
with
> Apple."
> - Byte Magazine
> 
> Given infinite time, 100 monkeys could type out the complete works of
> Shakespeare. Win 98 source code? Eight monkeys, five minutes.
> -- NullGrey 
> 
> 
> 
> -- 
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:    http://lists.mysql.com/mysql?unsub=1
> 

Thread
If statement in a where query....Mike Morton1 Dec
  • Re: If statement in a where query....SGreen1 Dec
  • Re: If statement in a where query....Rhino1 Dec
  • Re: If statement in a where query....yoge2 Dec