Reputation: 256
I have a table in a database with records containing keywords as well as other data. What would be a logical way to create a search function that would allow people to search based on keywords, and order results based on the number of matched keywords?
Upvotes: 0
Views: 231
Reputation: 75
Here is the query that will work for you.
SELECT *, MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE) AS relevancy
FROM table_name
WHERE MATCH (ab,cd) AGAINST ('sample text' IN BOOLEAN MODE)
ORDER BY relevancy DESC;
Upvotes: 1
Reputation: 5798
Mysql provide FULLTEXT
search options. Check this link mysql full text search. These search results will be sorted according to best match. it also has support for boolean mode and NATURAL LANGUAGE MODE(default). You need to add FULLTEXT
index on search column.
Upvotes: 2