Conditional MySQL Query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Conditional MySQL Query

Post 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") . "'";
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Post Reply