SQL 数据库 学习 025 查询-08 模糊查询 --- 怎样编写模糊查询语句
2016-10-17 07:03
549 查看
我的电脑系统:Windows 10 64位
SQL Server 软件版本: SQL Server 2014 Express
本篇博客里面使用了
创建一个
总结: 通配符:
格式:
匹配的条件通常含有通配符
表示任意0个或多个字符
表示任意单个字符
不是
注意:
匹配的内容必须使用单引号括起来。不能省略,也不能改用双引号。
请访问:http://www.aobosir.com/
SQL Server 软件版本: SQL Server 2014 Express
本篇博客里面使用了
scott库,如何你现在还没有添加这个库到你的服务器里面,请在查看本篇博客前,访问这篇博文来在你的服务器里面附加
scott库。
怎样编写模糊查询语句
select * from emp where ename like '%A%' --ename只要含有字母A就输出 select * from emp where ename like 'A%' --ename只要首字母是A的就输出 select * from emp where ename like '%A' --ename只要尾字母是A的就输出
select * from emp where ename like '_A%' --ename只要第二个字母是A的就输出
select * from emp where ename like '_[A-F]%' --把ename中第二个字符是A或者B或者C或者D或者E或者F的记录输出
select * from emp where ename like '_[^A-F]%' --把ename中第二个字符不是A也不是B也不是C也不是D也不是E也不是F的记录输出
创建一个
student表。(预备操作)
create table student ( name nvarchar(20) null, age int ); insert into student values ('张三', 68); insert into student values ('Tom', 66); insert into student values ('a_b', 22); insert into student values ('c%d', 44); insert into student values ('abc_fe', 56); insert into student values ('haobin', 25); insert into student values ('HapBin', 88); insert into student values ('c%', 66); insert into student values ('long''s', 100) select * from student;
select * from student where name like '%\%%' escape '\' --把name中包含有%的输出 select * from student where name like '%\_%' escape '\' --把name中包含有_的输出
总结: 通配符:
格式:
select 字段的集合 from 表名 where 某个字段的名字 like 匹配的条件
匹配的条件通常含有通配符
%
表示任意0个或多个字符
_(这个是下划线,不是减号)
表示任意单个字符
[a-f]
a到
f中的任意单个字符。只能是
a、
b、
c、
d、
e、
f中的任意一个字符
[a, f]
a或者
f
[^a-c]
不是
a、也不是
b,也不是
c的任意单个字符
注意:
匹配的内容必须使用单引号括起来。不能省略,也不能改用双引号。
escape '\'里面
escape后面
''里面的
\字符就自定义的:通配符。它可以替换成其他的字符(比如:
a、
b、
m等等)。
请访问:http://www.aobosir.com/
相关文章推荐
- mybitis 用不同数据库模糊查询的 sql 语句的写法
- 学习数据库必须掌握的54条SQL查询语句
- 学习数据库必须掌握的54条SQL查询语句
- 怎样用SQL语句查询一个数据库中的所有表?
- 当只有一个输入条件的时匹配数据库表中多个字段模糊查询的SQL语句
- 学习数据库必须掌握的54条SQL查询语句
- 数据库学习笔记(二)---SQL查询语句
- 学习数据库必须掌握的54条SQL查询语句
- 学习数据库必须掌握的54条SQL查询语句
- 学习数据库必须掌握的54条SQL查询语句
- 数据库-Oracle【SQL模糊查询语句(like) 及SQL Server 2005 导入和导出向导】
- 学习数据库必须掌握的54条SQL查询语句
- 数据库-Oracle【SQL模糊查询语句(like) 及SQL Server 2005 导入和导出向导】
- 学习数据库必须掌握的54条SQL查询语句
- SQL 数据库 学习 030 查询-13 --- 查询语句的顺序
- 怎样用SQL语句查询一个数据库中的所有表?
- 学习数据库必须掌握的54条SQL查询语句
- 学习 SQL 语句 - Select(3): 条件查询与模糊查询
- 学习 SQL 语句 - Select(3): 条件查询与模糊查询
- C#和SQL Server2000数据库连接,查询语句总结