PHP/MySQL - include plurals but exclude singulars

前端 未结 3 1525
挽巷
挽巷 2021-01-21 11:47

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.

相关标签:
3条回答
  • 2021-01-21 12:29

    You should consider using FULL TEXT SEARCH.

    This will solve your Bread/read issue.

    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

    0 讨论(0)
  • 2021-01-21 12:30

    Searching for %breads% would NEVER return bread or read, as the 's' is a required character for the match. So just eliminate the and clause:

    SELECT ... WHERE (field LIKE '%breads%')
    SELECT ... WHERE (field LIKE '%paperback book%');
    
    0 讨论(0)
  • 2021-01-21 12:39

    With MySQL you can use REGEXP instead of like which would give you better control over your query...

    SELECT * FROM table WHERE field REGEXP '\s+read\s+'
    

    That would at least enforce word boundaries around your query and gives you much better control over your matching - with the downside of a performance hit though.

    0 讨论(0)
提交回复
热议问题