0

I have a query that gets information using the LIKE

SELECT * FROM table WHERE column LIKE '%$search%'

Now if i search for "Hello there" it will return something like

Oh hello there
Hello there
Hello there how are you

But i want to sort it by exact match first so it should be like

Hello there
Hello there how are you
Oh hello there

With the exact match on top and the exact match at the start of the sentence after that

4

3 回答 3

13

尝试这个

    SELECT * FROM table1 WHERE column LIKE '%$search%'
    order by case when column like '$search%'  then 1        //hello in begining
                  when column like '%$search%' then 2        //hello in middle
                  when column like '%$search'  then 3 end    //hello in end

在这里观看演示

于 2013-07-20T14:23:29.957 回答
7

使用INSTR按列中搜索字符串的位置对结果进行排序:;

SELECT * FROM table 
WHERE column LIKE '%$search%'
order by INSTR(column, '$search')
于 2013-07-20T14:19:47.837 回答
-2
SELECT * FROM table WHERE column LIKE '%$search%' ORDER BY column
于 2013-07-20T14:17:51.377 回答