SQL中替代Like语句的方法

1、like '%string%' 和 like '%string' 不会使用列上的索引,且效率低,本文替代like主要对此情况进行
2、like 'string%' 和 like 'sting%string' 能够使用列上的索引

 

SqlServer数据库:

 

使用CHARINDEX函数

 

例:

替代前:
  
select tId, tName from tb_Test where tName like'%abc%"
替代后:
  select tId, tName from tb_Test where charindex('abc',tName)>0

 

Oracle数据库:

 

使用INSTR函数

 

例:

替代前:
  
select tId, tName from tb_Test where tName like'%abc%"
替代后:
  select tId, tName from tb_Test where instr(tName,'abc')>0

另:对like '%..'型,Oracle数据库可采用下述替代方案:
   
select tId, tName from tb_Test where reverse(tName) like reverse('%abc')


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。