sql语句实现模糊查询

1.数据库模糊查询

select * from bzj_xmmm where name LIKE CONCAT(#{name},'%')

select * from bzj_xmmm where name LIKE '%'||#{name}||'%'

SELECT * FROM [user] WHERE u_name LIKE ‘%三%’

SELECT * FROM [user] WHERE u_name LIKE ‘%三%’ AND u_name LIKE ‘%猫%’

2.like模糊查询,支持%和下划线匹配,%匹配多个字符,_匹配任意一个字符

示例:

1、查询名字中含有张的学生信息

select * from student where sname like ‘%张%’;

2、查询名字以张开头的学生信息

select * from student where sname like ‘张%’;

3、查询名字以人结尾的学生信息

select * from student where sname like ‘%人’;

4、查询名字中第二个字为心的学生信息

select * from student where sname like ‘_心%’;

5、查询名字中第三个字为心的学生信息

select * from student where sname like ‘__心%’;

3.下划线在sql中有特殊含义,所以查询信息中含有下划线时需要转义

示例:

select * from student where sname like ‘%_%’;

————————————————
原文链接:https://blog.csdn.net/zhaoguofeng996/article/details/128578393

你可能感兴趣的:(数据库,sql,数据库,mysql)