PHP/MySQL-包括复数但不包括单数 [英] PHP/MySQL - include plurals but exclude singulars
问题描述
我正在建立一个网站,要求包含多个单词但不包括单数单词,并包括较长的短语但不包含在其中找到的较短的短语.
I am building a site with a requirement to include plural words but exclude singlular words, as well as include longer phrases but exclude shorter phrases found within it.
例如:
-
搜索面包"应返回其中包含面包"的结果,而不是面包"或读取"的结果.
a search for "Breads" should return results with 'breads' within it, but not 'bread' or 'read'.
搜索平装书"应返回其中包含平装书"的结果,而不是平装书"或书".
a search for "Paperback book" should return results with 'paperback book' within it, but not 'paperback' or 'book'.
我尝试过的查询是:
SELECT * FROM table WHERE (field LIKE '%breads%') AND (field NOT LIKE '%bread%')
...,即使其中包含"breads"和"bread"的记录,也显然没有返回结果.
...which clearly returned no results, even though there are records with 'breads' and 'bread' in it.
我了解此查询失败的原因(我告诉它同时包含和排除相同的字符串),但我想不出适用于代码的正确逻辑以使其正常工作.
I understand why this query is failing (I'm telling it to both include and exclude the same strings) but I cannot think of the correct logic to apply to the code to get it working.
推荐答案
您应该考虑使用全文搜索.
这将解决您的面包/阅读问题.
This will solve your Bread/read issue.
我相信在这里使用通配符是没有用的.可以说您正在使用'%read%'
,现在它还会返回bread
,breads
等,这就是为什么我推荐Full Text Search
I believe use of wildcards here isn't useful. Lets say you are using '%read%'
, now this would also return bread
, breads
etc, which is why I recommended Full Text Search
这篇关于PHP/MySQL-包括复数但不包括单数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!