views:

99

answers:

4

I have an array with zipcodes indexed by distance. now i need to select pizza shops based on the zips, some thing like:

ziparray[]

foreach loop

SELECT * FROM location WHERE food='pizza' and zip='ziparray[]'

//do stuff

The zip array can grow up to 30 or 40 zips in time, which means 30 or 40 querys. Is there a better way to do this? i'm hoping to figure out Stored Procedures this way I can just send the parms in and have it send back the data(if i can).

A: 

How about using in ?

Zed
+4  A: 
SELECT *
FROM location
WHERE food='pizza'
  and zip IN(90210, 55555, etc..)
FM
A: 

You might want to consider transforming that array into an IN clause, such that your query becomes SELECT * FROM location WHERE food='pizza' and zip in ('03750','03532', .. other 50 zip codes)

nos
A: 

you could construct a query using a for loop. here's an example in PHP:

$zipArray = array();

$query = "SELECT * FROM location WHERE food='pizza' AND ( 1=2";


foreach($zipArray as $zip){
  $query .= "OR zip ='$zip' ";
}

$query .= ")";

the 1=2 part is a little sloppy, you could add an OR at the end of each addition in the foreach loop and use some function to cut off the last part of the string.

GSto