Hi there , in order to make things easier for users i want to add multiple keyword search to my site. so that in the input the user would do something like : " keyword1 keyword 2" ( similar to google for example. would i need to write a code that would parse that string and do queries based on that or is there something built in mysql that could do it?
+2
A:
One easy way to implement this would be:
SELECT * FROM table
WHERE column LIKE '%keyword1%'
OR column LIKE '%keyword2%';
But you may want to look at full text search functions.
Edit: Oops, forgot to deal with splitting the input string.
Depending on the language you are using (PHP?):
To get the keywords separated, assuming they are separated by spaces:
$keywords = explode(" ", $searchquery);
Then you could use a loop to create the SQL query:
$query = "SELECT * FROM table WHERE ";
for($i = 0; $i < count($keywords); $i++)
{
$query .= "column LIKE '%{$keywords[$i]}%'";
if($i < count($keywords) - 1) $query .= " OR ";
}
JYelton
2010-04-06 22:09:07
The mysql part i am familiar with ( or at least that one) but was curious if that's how search engines do it ? by breaking up the query string into many and searching using the "or" command? or maybe there is a smarter way that I dont know? thank you
salmane
2010-04-06 22:11:05
Sorry, I added some info to deal with splitting the input string. I assumed it was to be split by spaces, but I wasn't sure what programming language you are using.
JYelton
2010-04-06 22:19:51
Search engines do indeed use full-text searching. (Although it's usually a bit more advanced than the one in consumer SQL databases). The LIKE approach offers terrible scalability, so that's why it's not used. (Also, full-text search systems usually allow for some inaccuracy in spelling, which LIKE will not.)
Michael Madsen
2010-04-06 22:20:00