views:

67

answers:

2
+2  Q: 

SQL Like question

Is there a way to reverse the SQL Like operator so it searches a field backwards? For example, I have a value in a field that looks like this "Xbox 360 Video Game". If I write a query like below, it returns the result fine.

SELECT id FROM table WHERE title like "%Xbox%Game%"

However, when I search like this, it doesn't find any results.

SELECT id FROM table WHERE title like "%Video%Xbox%"

I need it to match in any direction. How can I get around this?

+6  A: 

How about:

SELECT id FROM table WHERE title like "%Video%" and title like "%Xbox%"

SqlACID
This doesn't work well because users are performing a search and I don't know how many keywords they are entering.
mike
@mike: You can't build a query with an arbitrary number of conditions? It shouldn't be any different from building a pattern match string
Duncan
+3  A: 

Another option

SELECT id FROM table WHERE title RLIKE "(Xbox|Video)"
macek
This worked perfectly. I never used rlike before. I'm going to have to read up on how well that performs, but it does work! thanks!
mike
This will match titles with either term, which may not be what you want, e.g. If the user types in "Playstation Game" it will still return "Xbox 360 Video Game"
Duncan