时间:2023-10-27来源:系统城装机大师作者:佚名
模糊查询是针对字符串操作的,类似正则表达式,没有正则表达式强大。
1. 单条件查询
1 2 |
//查询所有姓名包含“张”的记录 select * from student where name like '张' |
2. 多条件查询
1 2 3 4 |
//查询所有姓名包含“张”,地址包含四川的记录 select * from student where name like '张' and address like '四川' //查询所有姓名包含“张”,或者地址包含四川的记录 select * from student where name like '张' or address like '四川' |
通配符:_ 、% 、[ ]
1 2 3 4 |
//查询所有名字姓张,字长两个字的记录 select * from student where name like '张_' //查询所有名字姓张,字长三个字的记录 select * from student where name like '张__' |
1 2 3 4 |
//查询所有名字姓张,字长不限的记录 select * from student where name like '张%' //查询所有名字姓张,字长两个字的记录 select * from student where name like '张%' and len( name ) = 2 |
1 2 3 4 5 6 7 8 9 10 |
//查询所有名字姓张,第二个为数字,第三个为燕的记录 select * from student where name like '张[0-9]燕' //查询所有名字姓张,第二个为字母,第三个为燕的记录 select * from student where name like '张[a-z]燕' //查询所有名字姓张,中间为1个字母或1个数字,第三个为燕的名字。字母大小写可以通过约束设定,不区分大小写 select * from student where name like '张[0-9a-z]燕' //查询所有名字姓张,第二个不为数字,第三个为燕的记录 select * from student where name like '张[!0-9]燕' //查询名字除了张开头妹结尾中间是数字的记录 select * from student where name not like '张[0-9]燕' |
1 2 3 4 5 6 |
//查询姓名包含通配符%的记录 select * from student where name like '%[%]%' //通过[]转义 //查询姓名包含[的记录 select * from student where name like '%/[%' escape '/' //通过指定 '/' 转义 //查询姓名包含通配符[]的记录 select * from student where name like '%/[/]%' escape '/' //通过指定 '/' 转义 |
到此这篇关于SQL实现模糊查询的四种方法总结的文章就介绍到这了
2023-10-27
浅谈SELECT *会导致查询效率低的原因2023-10-27
SQL Server 数据库中的收缩数据库和文件操作2023-10-27
dbeaver配置SQL server连接实现我们经常在SQL Server中使用group by语句配合聚合函数,对已有的数据进行分组统计。本文主要介绍一种分组的逆向操作,通过一个递归公式,实现ungroup操作。代码和实现我们看一个...
2023-10-27
一、sql server导入.mdf方法 1.右击数据库,点击附加 2.点击添加 3.选择文件,点击确定 4.添加成功! 二、如果出现错误,处理方法...
2023-10-27