du5407 2014-11-08 08:35
浏览 78
已采纳

通过搜索令牌问题的MYSQL自定义查询顺序

Right now i'm building a custom query using php and mySql. Say I've a string Hello this is me. For this

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

So far it was working fine until recently I've found a issue. Whenever I ran this kind of query it generates results contains user's with full name like the search token and other matching tokens generated from search token. But the actual row which has perfect match with provided token "Hello this is me" is not showing in top.

Let me explain the results if i ran current query,

  1. "Hello how are you"
  2. "this is nixon"
  3. "Hello this is me"
  4. "oh hello there"
  5. "I'm me"
  6. "hello you there"

I want to show the actual result in the top, so the results will be look like this,

  1. "Hello this is me"
  2. "Hello how are you"
  3. "this is nixon"
  4. "oh hello there"
  5. "I'm me"
  6. "hello you there"

Can anyone please tell me what's the problem here? Or should I remove or add the query?

Thanks in advance,

Nixon

  • 写回答

2条回答 默认 最新

  • dongtang7347 2014-11-08 09:13
    关注

    This sounds like a job for a FULL TEXT INDEX!

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

    MySQL has now created a fuzzier search index for text than your chain of OR's. So, part of your query will look like:

     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
    

    The better the match, the higher confidence will be, so "Hello this is me" should be on top.

    This might be your query, cleaned up and untested:

    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条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题