2

I have a mysql query as follows.

$query="SELECT name,activity FROM appid 
where result > 5 AND name LIKE :term ORDER BY name ASC LIMIT 0,40";

$result = $pdo->prepare($query);

$result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
$result->execute();

What i want to do is this.

I have and entry like this that i want to find

'News & Weather'

However when i type

'news weather'

it of course will not find it. How can i be able to type that and retrieve that entry?

2
  • 1
    Create an array of the words splitting on the space and have an OR LIKE for each value in the array. Commented Oct 7, 2014 at 17:32
  • Simpler: Use regular expressions (see answer below) Commented Oct 7, 2014 at 18:08

2 Answers 2

2

Regular expressions can do the trick:

select *
from appid
where name rlike 'news|weather' -- Matches 'news' or 'weather'

Another example:

select *
from appid
where name rlike 'news.*weather' -- Matches 'news' and 'wether' 
                                 -- with any characters in-between or none at all
                                 -- (ordered)

Just one more:

select *
from appid
where name rlike '^news.{1,}weather$' -- Matches any text that starts with 'news'
                                      -- and has at least one character before
                                      -- ending with 'weather'

Regular espressions can be used to create very complicated filters on text fields. Read the link above for more information.


If you can add a full-text index to your table, Full-text search might be the better way to go with this. Specifically, a boolean Full-Text search:

select *
from appid
where match(name) against (+news +weather)
Sign up to request clarification or add additional context in comments.

1 Comment

This looks good but how to i implement this into my query that uses $_GET from user search?
0

I believe the only way possible are through code:

Option A: Replace the spaces in your query parameter with '%' in code, but that of course will make the multiple words ordered

Option B: Split your parameter on spaces and dynamically construct your query with as many LIKEs as needed, adding additional ":termN" parameters for each one.

4 Comments

I recommend you read about regular expressions in MySQL for a better option
Oh yeah, I forgot it has those; I don't really have a use for them in the data I regularly work with.
I said exactly the same when someone introduce them to me... and now I can't leave without them ;)
Most the time, I am searching for strings "starting with ABC" in indexed fields, so regex would be a detriment unless they've optimized it since the last time I checked so it can use an index when the expression begins with fixed text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.