Page 1 of 1

Conditional MySQL Query

Posted: Thu Jun 15, 2006 1:50 am
by Benjamin
I'm trying to write a conditional query for a banner manager, I think what I have just needs to be tweaked but I can't find anything in the manual.

In English:

if Impressions + TargetedImpressions is less than DisplayLimit or if DisplayLimit is equal to 0 pull the row.

Code: Select all

$Query  = "select `AdID` ";
    $Query .= "from `" . TABLE_BANNER_MANAGER . "` ";
    $Query .= "where `Status`='0' and IF(DisplayLimit != '0',(Impressions+TargetedImpressions) < DisplayLimit,1) ";
    $Query .= " and `ExpireDate` <= '" . date("Y-m-d") . "'";

Posted: Fri Jun 16, 2006 9:23 am
by printf
Dont you think AND / OR would be better. You could use if(), but the reasoning to use it doesn't make sense in this case. if(s) are better used for doing another operation based some column value. Like doing a complex math problems where you need to pass a different divider or multiplier based on a value found in a certain column, so as to return a different result based on the equation, not restrict the result like you are wanting to do here!

Code: Select all

... Status = 0 AND DisplayLimit = 0 OR (Impressions + TargetedImpressions) < DisplayLimit ...
pif!

Posted: Tue Jun 20, 2006 1:41 am
by Benjamin
Yeah that would be better, for some reason I didn't think it would work like that. I ended up doing it a different way but next time I'll try it that way.