LIKE語句
SELECT `column` FROM `table` where `condition` like `%keyword%'
事實上,可以使用 locate(position) 和 instr 這兩個函數來代替
一、LOCATE語句
SELECT `column` from `table` where locate(‘keyword’, `condition`)>0
二、locate 的別名 position
POSITION語句
SELECT `column` from `table` where position(‘keyword’ IN `condition`)
三、INSTR語句
SELECT `column` from `table` where instr(`condition`, ‘keyword’ )>0
locate、position 和 instr 的差別只是參數的位置不同,同時locate 多一個起始位置的參數外,兩者是一樣的。
mysql> SELECT LOCATE(‘bar’, ‘foobarbar’,5);
instr(str1,str2)
有兩種用法:一種是前面參數寫變量,后面寫列名;還有就是位置調換。兩種有不同的效果,instr(str1,str2)的意思是str2在str1中,如果后面寫變量前面寫列名,則表示搜出表中所有str1列中值包含str2變量的數據,這時候跟(列名 like '目標表里')的效果是一樣的;
instr還有一個需要注意的地方,就是對該函數返回結果值的判斷,不寫時默認是判斷 "> 0";共有三種可能,每種對應情況如下:
----------------------
instr(title,'name')>0 相當於 title like '%name%'
instr(title,'name')=1 相當於 title like 'name%'
instr(title,'name')=0 相當於 title not like '%name%'
速度上這三個比用 like 稍快了一點。