使用 LIKE % 进行任意单词搜索

发布于 2025-01-07 21:01:12 字数 921 浏览 0 评论 0原文

我有一个数据库表,其中有一个字段 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 StanleyJohn Smith< /code> 或 Stanley SmithStanley 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2025-01-14 21:01:12

我可能误解了,但您是否考虑过这样做:

SELECT * FROM customer WHERE
    CONCAT(firstname, " ", lastname) LIKE '%$pattern%'

如果用户输入多个单词(以空格分隔),只需将字符串拆分为单词并将查询修改为

SELECT * FROM customer WHERE
    CONCAT(firstname, " ", lastname) LIKE '%$word1%'
 OR CONCAT(firstname, " ", lastname) LIKE '%$word2%'
 OR CONCAT(firstname, " ", lastname) LIKE '%$word3%'
 ...

I might have misunderstood but have you considered just doing this:

SELECT * FROM customer WHERE
    CONCAT(firstname, " ", lastname) LIKE '%$pattern%'

if the user enters more than one word, separated by whitespaces, simple split the string into words and modify the query to

SELECT * FROM customer WHERE
    CONCAT(firstname, " ", lastname) LIKE '%$word1%'
 OR CONCAT(firstname, " ", lastname) LIKE '%$word2%'
 OR CONCAT(firstname, " ", lastname) LIKE '%$word3%'
 ...
∞觅青森が 2025-01-14 21:01:12

希望这有帮助

$whereclauses=array();

$terms = explode(' ', $search_term);
foreach ($terms as $term) {
    $term = mysql_real_escape_string($term);
    $whereclauses[] = "CONCAT(first_name, ' ', last_name) LIKE '%$term%'";
}
$sql = "select * from table where";
$sql .= implode(' and ', $whereclauses);

Hope this helps

$whereclauses=array();

$terms = explode(' ', $search_term);
foreach ($terms as $term) {
    $term = mysql_real_escape_string($term);
    $whereclauses[] = "CONCAT(first_name, ' ', last_name) LIKE '%$term%'";
}
$sql = "select * from table where";
$sql .= implode(' and ', $whereclauses);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文