现在我正在使用php和mySql构建自定义查询。说我有一个字符串你好,这是我。对于这个
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
到目前为止它工作正常,直到最近我才发现了一个问题。每当我运行这种查询时,它生成的结果包含全名的用户,如搜索令牌和从搜索令牌生成的其他匹配令牌。但是与提供的标记“Hello this is me”完美匹配的实际行并未显示在顶部。
如果我运行当前查询,请让我解释一下结果
我想在顶部显示实际结果,因此结果将如下所示,
有人可以告诉我这里有什么问题吗?或者我应该删除或添加查询?
提前致谢,
尼克松
答案 0 :(得分:2)
这听起来像是一份完整的文字索引!
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
匹配越好,confidence
越高,所以&#34;你好,这就是我&#34;应该是最重要的。
这可能是您的查询,清理和未经测试:
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
答案 1 :(得分:1)
添加匹配分数并按其排序:
select distinct(user_primary.id) as id,
CASE user_primary.name
WHEN user_primary.name like "%Hello this is me%" THEN 100
WHEN user_primary.name like "%Hello%" THEN 50
WHEN user_primary.name like "%this%" THEN 40
WHEN user_primary.name like "%is%" THEN 30
WHEN user_primary.name like "%me%" THEN 10
ELSE 0
END as sortScore
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 this is me%"
or 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.login <= 1415426357
order by sortScore
limit 0, 150