mysql 替换like查询的几种方法

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

  1. 标签: locate instr find_in_set    分类: MySQL  
  2. 你是否一直在寻找比MySQL的LIKE语句更高效的方法的,下面我就为你介绍几种。  
  3.   
  4. LIKE语句  
  5. SELECT `column` FROM `table` where `condition` like `%keyword%'  
  6.   
  7. 事实上,可以使用 locate(position) 和 instr 这两个函数来代替  
  8.   
  9. 一、LOCATE语句  
  10. SELECT `column` from `table` where locate(‘keyword’, `condition`)>0  
  11.   
  12. 二、或是 locate 的別名 position  
  13. POSITION语句  
  14. SELECT `column` from `table` where position(‘keyword’ IN `condition`)  
  15.   
  16. 三、INSTR语句  
  17. SELECT `column` from `table` where instr(`condition`, ‘keyword’ )>0  
  18.   
  19. locate、position 和 instr 的差別只是参数的位置不同,同时locate 多一个起始位置的参数外,两者是一样的。  
  20. mysql> SELECT LOCATE(‘bar’, ‘foobarbar’,5);  
  21.   
  22. -> 7  
  23.   
  24. 速度上这三个比用 like 稍快了一点。  
  25.   

猜你喜欢

转载自www.cnblogs.com/xiami2046/p/12823182.html