MySQL比like语句更高效的写法locate position instr find_in_set

来源:互联网 发布:php curl http post 编辑:程序博客网 时间:2024/04/30 04:37
你是否一直在寻找比MySQL的LIKE语句更高效的方法的,下面我就为你介绍几种。

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 稍快了一点。
阅读全文
0 0