使用 LIKE % 进行任意单词搜索
我有一个数据库表,其中有一个字段 firstname
和一个字段 lastname
。我正在尝试使用搜索框实现一个任意单词搜索系统,假设我们在数据库中有一个名为 John Stanley Smith
的用户(其中 firstname = John Stanley 和
lastname = Smith
),如果用户键入以下任何内容,我希望能够返回 John Stanley Smith
:
1- 属于 < 一部分的任何字母代码>约翰·斯坦利·史密斯 (j, s, l, e、y、m 等...)
2- 属于 John Stanley Smith
的任何单词 3- 作为 John Stanley Smith
一部分的任何单词组合(以任何顺序),这意味着,如果用户输入 John Stanley
或 John Smith< /code> 或
Stanley Smith
或 Stanley John
(你明白了)等等...我希望能够找到 John Stanley Smith
。
我知道我可以使用 MySQL 全文搜索 来做到这一点功能,但如果我这样做,第一点将不起作用(因为 MySQL 全文搜索功能仅匹配整个单词,而不匹配字符串中的子字符串)。此外,执行搜索所需的最小单词数(由 min_word_len
常量设置)。所以,我真的很想使用 LIKE % 来做到这一点。
谢谢
I have a database table which has a field firstname
and a field lastname
. I'm trying to implement an any-word search system using a search box where if let's say that we have a user named John Stanley Smith
in the database (where firstname = John Stanley
and lastname = Smith
), I want to be able to return John Stanley Smith
if the user types any of the following:
1- Any letter which is part of John Stanley Smith
(j, s, l, e, y, m, etc...)
2- Any word which is part of John Stanley Smith
3- Any combination of words (in any order) which is part of John Stanley Smith
, meaning that, if the user enters either John Stanley
or John Smith
or Stanley Smith
or Stanley John
(you get the idea) etc... I want to be able to find John Stanley Smith
.
I know I can do this using the MySQL Fulltext Search feature, but if I do, point number one will not work (because the MySQL fulltext search feature only matches whole words, not substrings within a string). Also, there is a minimum number of words required for the search to be performed (set by the min_word_len
constant). So, I really want to do this using LIKE %.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能误解了,但您是否考虑过这样做:
如果用户输入多个单词(以空格分隔),只需将字符串拆分为单词并将查询修改为
I might have misunderstood but have you considered just doing this:
if the user enters more than one word, separated by whitespaces, simple split the string into words and modify the query to
希望这有帮助
Hope this helps