2010年8月24日 星期二

Mysql 函數 LOCATE INSTR vs Like

Mysql 大家都會用這樣的語法: SELECT `column` FROM `table` where `condition` like `%keyword%'
事實上,可以使用 locate 和 instr 這兩個函數來代替
SELECT `column` from `table` where locate('keyword', `condition`)>0
或是 locate 的別名 position
SELECT `column` from `table` where position('keyword' IN `condition`)
或是
SELECT `column` from `table` where instr(`condition`, 'keyword' )>0
locate、position 和 instr 的差別只是參數的位置不同(請仔細看上例),同時locate 多一個請始位置的參數外,兩者是一樣的。
速度上這兩個函數比用 like 稍快了一點。
[參考資料]
Mysql String Function http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

【下列文章您可能也有興趣】

沒有留言: