multi word query in mysql
in order to make things easier for users i want to add multiple keyword search to my site. so that in the input the u开发者_如何学JAVAser 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?
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 ";
}
精彩评论