[thelist] MySQL "OR"

Hans Zaunere lists at zaunere.com
Thu Sep 8 08:10:36 CDT 2005



Andrew Kamm wrote on Thursday, September 08, 2005 8:51 AM:
> Is it proper to execute a rather long SQL query in MySQL?  I'm not aware
> of a reason this wouldn't work (unless there is simply a limit on the
> length of a queries), but I'm not sure if it's bad form or if the DB
> would respond poorly.
> 
> Please see the PHP code below.  At this point in the code, I've gathered
> an array of advertiser_id's from a separate function, and I want to do a
> query that basically says "SELECT X FROM TABLE Y WHERE advertiser_id =
> $q_or" where $q_or is the imploded array below.  As the app grows, this
> could become a potentially long list of "OR's".
> 
> if (is_array($advertisers))
>         {    //Wrap array values with ' '
>             foreach($advertisers as $value)
>             {
>                 $newAdvertisers[] = "'".$value."'";
>             }
> 
>             //Create 'or' section of query with array
>             $q_or = implode(" OR advertiser_id = ", $newAdvertisers);
> 
>             //SELECT STATEMENT, ETC....
>         }
> 
> Thanks in advance!

You don't want to over use the logical OR.  Also consider IN()

http://dev.mysql.com/doc/mysql/en/comparison-operators.html



---
Hans Zaunere / President / New York PHP / www.nyphp.com




More information about the thelist mailing list