MYSQL 模糊查询 LIKE

"_"是表明一个模糊字符,mysql

"%"是表明零~多个字符
若是不加这两个符号,那么like 和=就是同样的sql

1.包含    where name like ‘%明%’ 函数

2.以固定字符串开头结尾spa

        where name like ‘李%’    开头字符串

        where name like ‘%李’    结尾select

3.含有数字的 
    where name like ‘%[0-9]%’
4.不含有数字
    where name like ‘%[!0-9]%’方法

5.含有小写字母的 where name like ‘%[a-z]%’查询

引伸:模糊查询带下划线 “_”的字符co

方法1:使用escape转义字符

mysql> select * from t where x like '%\_%' escape '\';

返回包含有"_"的记录,正确

escape的内容能够任意,只要保证先后一致便可。

mysql> select * from t where x like '%|_%' escape '|';

返回包含有"_"的记录,正确

mysql>  select * from t where x like '%*_%' escape '*';

返回包含有"_"的记录,正确

方式2:instr函数辅助判断

select * from t where instr(x,'_') !=0;

(备注:使用instr函数判断字段中是否包含“_”,若是包含返回值是非零的,若是不包含则返回值是零。)

相关文章
相关标签/搜索