Source
Source

Reputation: 1011

Search for keywords in more than one fields of mysql database

I want to perform a site search. The thing is that I have 3 columns I want to search, and a row will be displayed in the results if the search query is any of these fields. By which I mean that if someone searches "keyword1 keyword2 keyword3", the columns we search are page name, title and description, so if keyword1 was only in the title, keyword2 was only in the name, and keyword3 was only in the description, that would be a valid result.

I just can't figure out the logic behind this. I tried using regex in the sql with the OR operator, but this still required all the keywords in either the title, name or description. Any idea how I accomplish this?

Upvotes: 0

Views: 436

Answers (1)

MJ Khan
MJ Khan

Reputation: 1746

Please look at MySQL LIKE IN()?

You can replace the spaces with | using str_replace and build a query like:

SELECT * from mytable where pagename REGEXP 'keyword1|keyword2|keyword3' and title REGEXP 'keyword1|keyword2|keyword3' and description REGEXP 'keyword1|keyword2|keyword3';

Upvotes: 2

Related Questions