简体   繁体   English

MySQL在单词上选择(复数,单数)

[英]Mysql select on word (plural, singular)

In my Mysql DB I have a list of terms like (With First letters Capital and most of the time plurals) 在我的Mysql DB中,我有一个术语列表,例如(带有首字母大写和大多数时间的复数形式)

Hairdressers
Restaurants
Beauty Salons 
Fournitures For Restaurants

On my website I have a searchbar where the user can search for those word (my website is a kind of POI finder). 在我的网站上,我有一个搜索栏,用户可以在其中搜索那些词(我的网站是一种POI查找器)。 But I'm having problems with my query. 但是我的查询有问题。

SELECT * FROM TABLE WHERE word = 'userword'

1: If the user enter restaurants, it doesn't work because he put 'r' on lower case. 1:如果用户进入餐厅,则不会起作用,因为他将“ r”放在小写字母上。 2: if the user enter restaurant either because because he didn't put it in plural 2:如果用户进入餐厅是因为他没有将餐厅输入复数形式

I have tried with SELECT * FROM TABLE WHERE word like 'userword' 我已经尝试使用SELECT * FROM TABLE WHERE word like 'userword'

This fix the problem for the Capital leters, but I have no idea how to make the query work when the user enter the word in singular. 这解决了大写字母的问题,但是我不知道当用户以单数形式输入单词时如何使查询正常工作。 is the only solution add that word in DB as well ? 唯一的解决方案也是在DB中添加该单词吗?

Thank you 谢谢

Maybe it is better with a regular expression that match also plural name testing only for an additional s at the end of the word : 也许最好使用一个正则表达式,使其在单词的末尾仅对另外一个s也匹配复数名称测试:

SELECT * FROM table where word REGEXP '^userword[s]?'

Note that regular expression match in MySQL are case insensitive by default. 请注意,默认情况下,MySQL中的正则表达式匹配不区分大小写。

When you use LIKE you can use % to signify wildcards 使用LIKE时,可以使用%表示通配符

So: 所以:

SELECT * FROM table where word like '%userword%'

Matches: 火柴:

userword, userwords, superuserwords, ...

But if you use LIKE thus (note the additional s ): 但是如果你使用LIKE这样(注意额外S):

SELECT * FROM table where word like '%userwords%'

It does not match: 匹配:

userword, superuserword, ...

The best solution is to use a proper search engine for this which will use an appropriate analyser and a stemmer in order to serve quality results back to the users. 最好的解决方案是为此使用适当的搜索引擎,该引擎将使用适当的分析器和词干分析器,以便将质量结果返回给用户。 Such a search engine is lucene and if you don't want to deal with implementation details you can use Solr which is a search server. 这样的搜索引擎是lucene ,如果您不想处理实现细节,可以使用搜索服务器Solr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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