简体   繁体   中英

MySQL and PHP search for similar words

My MySQL table contains items which have the attributes name(name of item) and keywords(words related to the item). I search the database with the following code..

SELECT * FROM items WHERE (name LIKE '%$query%' OR keywords LIKE '%$query%') LIMIT $start, $per_page"

One item is a phone and it has a keyword "mobile", so when someone searches mobile the item appears as a result which is fine. But when someone searches "mobiles" with the s on the end it displays no results. How can I make my query search for anything that includes the search term?

Assuming this was down-voted due to lack of detail, I am editing with an explanation.

The Like operator matches a strict pattern of characters with some wildcard characters. What you are asking the database to do is interpret natural language variants on search terms to account for things like plurals. You cannot do that with pattern matching alone. There is no good, reliable way to match on a part of a search term with the Like or similar operator (we can write code to remove any trailing spaces in the search in an attempt to match on more items, for example). I recommend you research the following, a capability common to all major RDBMS platforms such as MS SQL:

Natural Language Full-Text Searches:

http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

That link is a reference to the feature complete with sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM