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

来源:互联网 发布:网红美图软件有哪些 编辑:程序博客网 时间:2024/04/30 05:09

1、LIKE
      SELECT `column` FROM `table` where `condition` like`%keyword%'


事实上,可以使用 locate(position) 和 instr这两个函数来代替

2、LOCATE语句
     SELECT `column` from `table` where locate(‘keyword’,`condition`)>0
3、POSITION语句(LOCATE 的別名 POSITION)
      SELECT `column` from `table` where position('keyword' IN `condition`)

4、INSTR语句
      SELECT `column` from `table` where instr(`condition`,'keyword')>0


     locate、position 和 instr 的差別只是参数的位置不同,同时locate多一个起始位置的参数外,两者是一样的。
     mysql> SELECT `column` from `table` where LOCATE(‘bar’, ‘foobarbar’,5);


5、FIND_IN_SET语句
      find_in_set(str1,str2) 函数:返回str2中str1所在的位置索引,其中str2必须以","分割开。
      mysql> select * from test where find_in_set('name1',name);

0 0
原创粉丝点击