2

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?

2 Answers 2

7

How about:

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

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work well because users are performing a search and I don't know how many keywords they are entering.
@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
5

Another option

SELECT id FROM table WHERE title RLIKE "(Xbox|Video)" 

2 Comments

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!
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"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.