If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Multi-value "LIKE" Statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-08, 16:17
smack78 smack78 is offline
Registered User
 
Join Date: Feb 2008
Posts: 8
Multi-value "LIKE" Statement

I looked through Google but I couldn't find an answer. I'm sure this is a fairly simple question.

I was wondering if there was a way to do a statement with several LIKE values.

Example:

SELECT DISTINCT state_name
FROM `zip_code`
WHERE zip_code LIKE ('010%', '120%', '481%')

Here I have a table with zip codes and state names (obviously). I want to get the state names for zip codes that start with 3 specific digits.

Any one have a solution?

Thank you,
Smack
Reply With Quote
  #2 (permalink)  
Old 02-24-08, 17:06
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Try
Code:
SELECT DISTINCT state_name
FROM `zip_code`
WHERE zip_code LIKE '010%'
         or  zip_code LIKE  '120%'
         or zip_code LIKE '481%'
There are loads of tutorials on SQL on the web.

Mike
Reply With Quote
  #3 (permalink)  
Old 02-24-08, 20:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
change this --

WHERE zip_code LIKE ('010%', '120%', '481%')

to this --

WHERE left(zip_code,3) IN ('010', '120', '481')

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-25-08, 03:52
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Have you left your NZDF flag permanently on Rudy?
Reply With Quote
  #5 (permalink)  
Old 02-25-08, 04:41
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
OOh i like that solution rudy, what's the performance impact between the 2 (i'm not expecting statistics, just an educated guess at the difference and why)?

p.s. would it make sense to have an index on the first 3 letters of that column ?
Code:
CREATE INDEX partial_zip ON zip_code (zip_code(3))
Reply With Quote
  #6 (permalink)  
Old 02-25-08, 07:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
i would just index the column

i think i read somewhere that mysql is optimized so that LEFT(xxx,n) will use an index just like LIKE will if there is no leading wildcard
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On