Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Mysql where IN() with pattern matching
Old 03-11-2009, 04:04 PM Mysql where IN() with pattern matching
Junior Talker

Posts: 2
Trades: 0
Hi guys,

I can't seem to figure this one out at the moment, hope you can help me. I'm trying to query a MySQL server with "where row IN('item1','item2','item3')" but would also like to be able to use pattern matching, "where row LIKE 'item1%'".

This returns an empty result:
Code:
select * from table where row IN('item1%','item2%','item3%')
I can't seem to figure out how I can do this. Can I combine LIKE and IN in some way or is this just not possible with MySQL?

Hope you guys have some good answers,
Sincerely,
Trymbill
trymbill is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-11-2009, 04:22 PM Re: Mysql where IN() with pattern matching
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Use OR to add more criteria to the query

WHERE column IN([csv_list]) OR column LIKE wildcard_criteria OR column LIKE wildcard_criteria
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-11-2009, 08:56 PM Re: Mysql where IN() with pattern matching
Junior Talker

Posts: 2
Trades: 0
Thanks for the response!

Just to get things straight, I can't do "where row IN('%text%')" with MySQL? I have to split up the array I would've liked to put into the IN function and basically query "where row like '%text1%' or row like '%text2%' or row like '%text3%'" ??
trymbill is offline
Reply With Quote
View Public Profile
 
Old 03-13-2009, 06:03 AM Re: Mysql where IN() with pattern matching
Junior Talker

Posts: 2
Name: Lee
Trades: 0
What about storing your items to search for as an array, then foreach() through them. Each time adding your "field LIKE '$item%' or" something like
Code:
 foreach($array as $item) {  $where .= "field LIKE '".$item."%' OR";  }
Its up to you how you handle the last OR, this code will produce an OR on the end which needs to be removed, you could substr() after the loop to remove it, or maybe write in a check (id use a ternary statement if doing it this way), and a counter so if the counter indicates you are on your last item in the array (use count to get number of items if needed), you dont add the OR at the end But thats just how id get around the problem, maybe its good maybe its not, but it works lol (im no php guru)
djlee is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Mysql where IN() with pattern matching
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20369 seconds with 12 queries