MySQL order by relevance

后端 未结 4 1457
感动是毒
感动是毒 2021-01-02 08:40

I have a search form which searches a site content table to pull back appropriate results.

I want to search the title and content fields and pull back results in ord

4条回答
  •  春和景丽
    2021-01-02 09:06

    mysql fulltext search is a good thing but it has a limit of minimum 4 characters word to be indexed. Al tough the limit can be changed but changing server variables isn't possible in all scenarios. In such a situation, I recommend the solution suggested in order by case

    select 
        *
    from
    mytable a
    WHERE
        (a.title like 'somthing%'
        OR a.title like '%somthing%'
        OR a.title like 'somthing%')
    ORDER BY case
    WHEN a.title LIKE 'somthing%' THEN 1
    WHEN a.title LIKE '%somthing%' THEN 2
    WHEN a.title LIKE '%somthing' THEN 3
    ELSE 4 END;
    

提交回复
热议问题