tags:

views:

381

answers:

5

Is there an elegant way to do this:

SELECT Cols from MyTable WHERE zip = 90210 OR zip = 23310 OR zip = 74245 OR zip = 77427 OR zip = 18817 OR zip = 94566 OR zip = 34533 OR zip = 96322 OR zip = 34566 OR zip = 52214 OR zip = 73455 OR zip = 52675 OR zip = 54724 OR zip = 98566 OR zip = 92344 OR zip = 90432 OR zip = 91532 OR ...

(zip codes in this post are ficticious and bear no resemblance to actual zip codes living or dead)

+25  A: 

Yes:

Select cols from MyTable where zip in (90210, 23310, ... etc.)

Ben

Ben
Even better would be to store the ZIPs in a table and embed a select in the IN clause.
Unsliced
*Even* better would be to use a 'where exists' or a join instead of 'in'
annakata
+15  A: 

Depends on the definition of "elegant" :)

However, with that many ZIP codes, I think you might want to manage them in the DB too.

How do you decide which ZIP codes to match against?

So you could put the ZIP codes in a table of their own and do

SELECT cols FROM MyTable, ZipTable WHERE MyTable.zip = ZipTable.zip

Paul
I agree. This type of query is usually a sign that you are missing a table or a column, or you are trying to do what databases are there for: to join stuff.
WW
+8  A: 

Or a combination of both, a nested query:

SELECT cols FROM MyTable WHERE zip IN 
    (SELECT zip FROM ZipTable WHERE condition=true)
Michael Borgwardt
In MySQL IN is less effective than EXISTS, so (if MySQL is used) it would be better to write: SELECT cols FROM MyTable M WHERE exists (SELECT 1 FROM ZipTable Z WHERE M.ZIP=Z.ZIP and condition=true)
Abgan
A: 

What Ben said. You should also be able straight up pass an array in place of that (90210, 23310, ... etc.) there.

Stephen Belanger
+3  A: 

I'd use something like:

start transaction; (don't remember the idiom for it)

create temporary table if not exists ZIPS(ZIP integer) storage=memory;

insert into ZIPS (ZIP) VALUES(the values you want to insert)

select COLS from MYTABLE M, ZIPS Z where Z.ZIP = M.ZIP

drop table ZIPS (or truncate it or do whatever you wish)

either commit or rollback

In every db API you should have some variation of executemany function, which can call insert into TABLE(COLUMNS) VALUES with multiple value rows and be faster than repeating single insert. And you can wrap such call sequence in some function, for easy reuse, as a temporary table with one INT column is often handy :-)

This way you can avoid problem with maximum SQL query length problem (e.in g. MySQL) and your query is effective, clean and easy to maintain or extend further.

Abgan