I have a query like:
SELECT ID FROM SomeTable WHERE ZipCode IN (SELECT Zip FROM SomeOtherTable)
This works as expected, however I need have a LIKE command. Some US zip codes are XXXXX-XXXX. I need the statement to match the first 5 characters only, even if it has more.
My PHP zip code check is:
preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip_code)
to show the format of how the zips are checked to be stored.
I have read about there being no IN LIKE command and to use REGEXP, but I am not sure how to use REGEXP with a select statement.
How would I go about changing this MySQL statement to match zips by the first 5 characters only?
You can change the query to:
By the way, if SomeOtherTable has more than one record per zip code, you may be able to speed things up by adding DISTINCT to the inner query so it produces just a unique list of zip codes. See the SELECT documentation.