MySQL中经常用like
进行模糊查询,而模糊查询就要用到百分号“%
”,下划线“_
”这些通配符,其中“%
”匹配任意多个字符,“_
”匹配单个字符。
如果我们想要模糊查询带有通配符的字符串,如“60%
”,“user_name
”,就需要对通配符进行转义。
使用转义字符‘\’
select * from student where sname like 'li\%';
使用ESCAPE转义
select * from student where sname like 'li#%' escape '#';
注意在MySQL中
select * from student where sname like 'li/%' escape '/';
会出错,原因应该是/
是系统定义转义字符,不能重复定义。
使用INSTR替换like
select * from student where INSTR(sname,'li%');