instr(sourceString,destString,start,appearPosition)
sourceString:目标字段,需要进行匹配的字段
destString:匹配字段,用来与目标字段匹配的字段
start:决定从目标字段开始匹配的位置,默认为1(提示:如果start为负数,则代表从目标字段的右边开始匹配,但是返回的位置数值还是从左开始计数。默认从左向右。)
appearPosition:表示匹配次数,默认为1
函数返回值为匹配字段的位置数值
下面用一些例子来理解instr()函数。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1gjM3UDM0YTMxADNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
instr()函数-模糊查询
select * from a where instr(a.name,'q')>0
-- 等同于
select * from a where a.name like '%q%'
instr()函数-判断关系(全集,字集)
select * from a where instr('qqq,ww,e',a.name)>0
-- 等同于
select * from a where a.name in ('qqq','qq','q','ww','w','e')
配合upper()和lower()函数可以实现无视大小写匹配。
思路:将目标字符串和匹配字符串同时转大写或同时转小写再进行匹配。
select * from a where instr(lower(a.name),lower('SfdgdR'))
-- 或者
select * from a where instr(upper(a.name),upper('SfdgdR'))