简体   繁体   中英

Match comma seperated list with input in SQL statement

Im optimizing my SQL statement to make it faster.

I have a comma seperated list with zipcodes like

1111, 1112,1115,1112 etc etc

Now in my query I want to match if the iput matches 1 of those zipcodes. If so.. then it will return a ID of the object that has all those zipcodes.

But what is the best way to do this now im doing

AND ( loc.loc_zip LIKE  '%".$_REQUEST['zip']."%'

Validation of the input will be added ofcourse.. but this is just for testing. But I have tested this and it seems a bit slow.

Is this the best way to do this ?

you should use 'in'

select * from Users where userid in (1,2,3,4,45,6,656)

Edit: if the ZipCodes are Chars, you can only use in if you separate them by ''

select * from loc where loc.loc_zip in ('1111','1112','1115','1112')

if .$_REQUEST['zip']. has ' ' then->

select * from loc where loc.loc_zip in (.$_REQUEST['zip'].)

if the ZipCodes are int you can use the first statement Bare in mid you must intersect your list with '' or it wont work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM