使用內部函數instr,可代替傳統的like方式查詢,並且速度更快。
instr函數,第一個參數是字段,第二個參數是要查詢的串,返回串的位置,第一個是1,如果沒找到就是0.
例如,
select name from tpl_user where 1 and instr(`name`,’jack’);
可查詢名字中帶jack的名字。
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);
-> 7
速度上這三個比用 like 稍快了一點。