php - MYSQL 通过搜索 token 问题自定义查询顺序

标签 php mysql sql database

现在我正在使用 php 和 mySql 构建自定义查询。假设我有一个字符串 Hello this is me。为此

select distinct(user_primary.id) as id 
from user_primary, 
user_sec, 
user_location, 
user_ind_cat 
where 
( 
 user_primary.status != 3 
 and 
 (
  user_primary.id = user_sec.job_id
 ) 
 and 
 (
  user_primary.id = user_ind_cat.job_id
 )
) 
and 
( 
 (
  user_primary.name like "%Hello%"
 ) 
 or 
 (
  user_primary.name like "%this%"
 ) 
 or 
 (
  user_primary.name like "%is%"
 ) 
 or 
 (
  user_primary.name like "%me%"
 ) 
 and 
 (
  user_primary.name like "%Hello this is me%"
 )
) 
and 
(
 user_primary.login <= 1415426357
) 
limit 0, 150

到目前为止一切正常,直到最近我发现了一个问题。每当我运行这种查询时,它都会生成包含用户全名的结果,例如搜索 token 和从搜索 token 生成的其他匹配 token 。但是与提供的标记“你好,这是我”完美匹配的实际行并未显示在顶部。

如果我运行当前查询,让我解释一下结果,

  1. “你好,你好吗”
  2. “这是尼克松”
  3. “你好,我是”
  4. “哦你好”
  5. “我就是我”
  6. “你好”

我想在顶部显示实际结果,所以结果看起来像这样,

  1. “你好,我是”
  2. “你好,你好吗”
  3. “这是尼克松”
  4. “哦你好”
  5. “我就是我”
  6. “你好”

谁能告诉我这里有什么问题?还是应该删除或添加查询?

提前致谢

尼克松

最佳答案

这听起来像是全文索引的工作!

 ALTER TABLE `user_primary` ADD FULLTEXT INDEX (`name`);

MySQL 现在已经为文本创建了一个比您的 OR 链更模糊的搜索索引。因此,您的部分查询将如下所示:

 SELECT name, MATCH(name) AGAINST ('Hello this is me') as confidence 
     FROM user_primary
     WHERE MATCH(name) AGAINST ('Hello this is me')
     ORDER BY confidence DESC

匹配越好,置信度就越高,所以“Hello this is me”应该放在最前面。

这可能是您的查询,已清理且未经测试:

select distinct(user_primary.id) as id , MATCH(name) AGAINST ('Hello this is me') as confidence
from user_primary, user_sec, user_location, user_ind_cat 
WHERE 
 user_primary.status != 3 
 and user_primary.id = user_sec.job_id 
 and user_primary.id = user_ind_cat.job_id
 and MATCH(name) AGAINST ('Hello this is me')
 and  user_primary.login <= 1415426357
ORDER BY confidence DESC
limit 0, 150

关于php - MYSQL 通过搜索 token 问题自定义查询顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815291/

相关文章:

php - 反转 mysql_real_escape_string

mysql - SQL 语法配置值选择

sql - 从不相关的表中一次插入两个或多个值

mysql - 统计特定月份每天的所有记录

mysql - MySQL 中累积列的限制

php - 正则表达式递归代码块内容

PHP - MySQL 包装类建议

mysql - 从 mysql 中创建的列中提取 timediff

php - 使用 PHP 从文件夹中获取图片名称 - 然后在其中查找数字,然后分别将每个数字插入 MySQL 表中

php - 自定义 MVC 框架 : How set links in view files