2013-07-20 52 views
0

我有一個查詢,獲取使用LIKEMysql的如訂單卜精確匹配

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

信息現在,如果我搜索「你好」,它將返回類似

Oh hello there 
Hello there 
Hello there how are you 

但我想先按完全匹配排序,所以它應該像

Hello there 
Hello there how are you 
Oh hello there 

在上面的確切匹配和開始時的確切匹配這

回答

9

試試這個

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 

watch demo here

+0

不返回任何東西,嗯 – user2458791

+0

不返回任何東西?檢查我的答案,並看看演示 –

+0

我做了,我改變了你好我的變量和改變列名稱,它不會返回任何東西 – user2458791

-2
SELECT * FROM table WHERE column LIKE '%$search%' ORDER BY column 
+0

沒有給我相同的輸出 – user2458791

6

使用INSTR後的句子由列搜索字符串的位置訂購結果:;

SELECT * FROM table 
WHERE column LIKE '%$search%' 
order by INSTR(column, '$search') 
+0

十分感謝,是有辦法,我可以通過訂購的精確匹配後的結果,如果該字符串開頭的句子,像「你好,你」會在「你好,你好」 – user2458791

+0

@ user2458791:你可以使用'INSTR()'。查看我的更新。 –