Mysql: Order by like?

前端 未结 5 1974
抹茶落季
抹茶落季 2020-11-29 00:35

assume that we are performing search using keywords: keyword1, keyword2, keyword3

there are records in database with column \"name\":

1: John Doe
2: Samue         


        
5条回答
  •  旧时难觅i
    2020-11-29 01:16

    To build on RedFilter's answer, you could make the rows that have both keywords to be at the top:

    order by case 
    when (name LIKE "%John%" and name LIKE "%Doe%") then 1 
    when name LIKE "%John%" then 2
    when name LIKE "%Doe%"  then 3
    end
    

提交回复
热议问题