Mysql select on word(复数、单数)
在我的 Mysql DB 中,我有一个术语列表,例如(首字母大写,大多数时候是复数)
Hairdressers
Restaurants
Beauty Salons
Fournitures For Restaurants
在我的网站上,我有一个搜索栏,用户可以在其中搜索这些单词(我的网站是一种 POI 查找器)。但我的查询遇到了问题。
SELECT * FROM TABLE WHERE word = 'userword'
1:如果用户输入餐馆,则不起作用,因为他将“r”改为小写。 2:如果用户输入餐厅是因为他没有将其以复数形式输入,
我已尝试使用 SELECT * FROM TABLE WHERE word like 'userword'
这解决了大写字母的问题,但我不知道当用户输入单数单词时如何使查询工作。唯一的解决方案是在数据库中添加该词吗?
谢谢
In my Mysql DB I have a list of terms like (With First letters Capital and most of the time plurals)
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). 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.
2: if the user enter restaurant either because because he didn't put it in plural
I have tried with 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 ?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许使用正则表达式来匹配复数名称测试(仅针对单词末尾的附加 s)会更好:
请注意,MySQL 中的正则表达式匹配默认情况下不区分大小写。
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 :
Note that regular expression match in MySQL are case insensitive by default.
最好的解决方案是使用适当的搜索引擎,该引擎将使用适当的分析器和词干分析器,以便为用户提供高质量的结果。这样的搜索引擎是lucene,如果你不想处理实现细节,你可以使用Solr 这是一个搜索服务器。
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.
当您使用 LIKE 时,您可以使用 % 来表示通配符
因此:
匹配:
但如果您这样使用 LIKE(请注意附加的 s):
它不匹配:
When you use LIKE you can use % to signify wildcards
So:
Matches:
But if you use LIKE thus (note the additional s):
It does not match: