【问题标题】:Mysql PHP find name in a textMysql PHP在文本中查找名称
【发布时间】:2014-06-28 16:53:21
【问题描述】:

我想知道是否有一种快速的方法来获得这样的一行:

Name (Database comumn) would be a database like |ID|Name|Age|Gender|

不在表中:

Text (long string)

姓名:

John Smith
Jack Stone
Brian Woods
Text: "Someone was going to go to John Smith but instead went to Brian Woods. Why would he do that to John Smith?"

查询应该返回“John Smith”匹配 2 次,“Brian Woods”匹配 1 次

你是如何得到这么快的?

$text = "Someone was going to go to John Smith but instead went to Brian Woods. Why would he do that to John Smith?";
$query = $mysql("SELECT * FROM persons");
while($r = mysql_fetch_assoc($query))
{
if(preg_match("/".$r["Name"]."/", $text))
{
$matches[$r["name"]]+=1;
}
}
print_r($matches);

【问题讨论】:

  • 是的,有可能。但是,您应该提供无效的代码。
  • 添加了它,很抱歉它确实有效,但在包含 3000 条记录的数据库中,这太慢了。

标签: php mysql sql pattern-matching match


【解决方案1】:

您在寻找like 命令吗?

SELECT COUNT(*)
FROM persons
WHERE name like '%John Smith%';

如果您开始获取大量数据(并且 3000 行并不是很多数据),您可能需要研究全文索引。

【讨论】:

  • 我不需要在数据库中找到它我需要知道一个文本中是否以及有多少个尖齿名称。不过还是谢谢你!
  • @MyName 。 . .然后只需使用count(*) 而不是*
  • 文本不在数据库中
【解决方案2】:

根据您的句子长度(与您要搜索的姓名数量相比,您可以通过以下方法翻转搜索方向)

你可以convert your sentence into an array of words(字符串)。现在遍历单词,将每个单词与下一个单词连接,然后在数据库的名称列表中搜索 in_array()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多