salmane
salmane

Reputation: 4849

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 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?

Upvotes: 0

Views: 924

Answers (1)

JYelton
JYelton

Reputation: 36512

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 ";
}

Upvotes: 2

Related Questions