在搜索数据库中的数据时,sql 通配符可以替代一个或多个字符。
sql 通配符必须与 like 运算符一起使用。
在 sql 中,可使用以下通配符:
通配符 描述
% 替代一个或多个字符
_ 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist]
或者
[!charlist]
不在字符列中的任何单一字符
-- sql模糊查询,使用like比较字,加上sql里的通配符,请参考以下:
-- 1、like'mc%' 将搜索以字母 mc 开头的所有字符串(如 mcbadden)。
-- 2、like'%inger' 将搜索以字母 inger 结尾的所有字符串(如 ringer、stringer)。
-- 3、like'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 bennet、green、mcbadden)。
-- 4、like'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 cheryl、sheryl)。
-- 5、like'[ck]ars[eo]n' 将搜索下列字符串:carsen、karsen、carson 和 karson(如 carson)。
-- 6、like'[m-z]inger' 将搜索以字符串 inger 结尾、以从 m 到 z 的任何单个字母开头的所有名称(如 ringer)。
-- 7、like'm[^c]%' 将搜索以字母 m 开头,并且第二个字母不是 c 的所有名称(如macfeather)。
提到like语句大家都很熟悉,比如查找用户名包含有c的所有用户, 我们可以用 use mydatabase
代码如下 复制代码
select * from table1 where username like'%c%
--查询包含%并且以a-z结尾的数据
select top 100 * from dbo.tablename with(nolock)
where colname like '%%%[a-z]' escape ''
--查询以%结尾的数据
select top 100 * from dbo.tablename with(nolock)
where colname like '%%%' escape ''
--查询包含50'的数据
select top 100 * from dbo.tablename with(nolock)
where colname like '%50''%'
--查询包含'的数据
select top 100 * from dbo.tablename with(nolock)
where colname like '%''%'
--查询包含有下划线的colvalue
select top 100 * from dbo.tablename with(nolock)
where colname like '%_%' escape ''
--查询包含有破折号的colvalue
--like '%-%--%'中的第一个破折号实际退出字符,紧接后面的%是常量字符数据值;
--第二个破折号是退出字符;
--第三个破折号是常量字符数据值。
select top 100 * from dbo.tablename with(nolock)
where colname like '%-%--%' escape '-'
--查询开头不是a-z,以e结尾的数据
select top 100 * from dbo.tablename with(nolock)
where colname like '[a-z]e'
--查询以tes开头,不以t-e结尾的数据
select top 100 * from dbo.tablename with(nolock)
where colname like 'tes[^t-e]%'
--查询第二个字母是a的colvalue
select top 10 * from dbo.tablename with(nolock)
where colname like '_a%'
--查询第三个字符为a,且长度为20个字符的colvalue
select top 10 * from dbo.tablename with(nolock)
where colname like '__ayboy - naked sins'
regex用法
“.”匹配任何单个的字符。
一个字符类“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的一个范围,使用一个“-”。“[a-z]”匹配任何小写字母,而“[0-9]”匹配任何数字。
“ * ”匹配零个或多个在它前面的东西。例如,“x*”匹配任何数量的“x”字符,“[0-9]*”匹配的任何数量的数字,而“.*”匹配任何数量的任何东西。
正则表达式是区分大小写的,但是如果你希望,你能使用一个字符类匹配两种写法。例如,“[aa]”匹配小写或大写的“a”而“[a-za-z]”匹配两种写法的任何字母。
如果它出现在被测试值的任何地方,模式就匹配(只要他们匹配整个值,sql模式匹配)。
为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用“^”或在模式的结尾用“$”。
为了说明扩展正则表达式如何工作,上面所示的like查询在下面使用regexp重写:
为了找出以“b”开头的名字,使用“^”匹配名字的开始并且“[bb]”匹配小写或大写的“b”:
代码如下 复制代码
mysql> select * from pet where name regexp ^[bb];
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| buffy | harold | dog | f | 1989-05-13 | null |
| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
为了找出以“fy”结尾的名字,使用“$”匹配名字的结尾:
mysql> select * from pet where name regexp fy$;
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| fluffy | harold | cat | f | 1993-02-04 | null |
| buffy | harold | dog | f | 1989-05-13 | null |
+--------+--------+---------+------+------------+-------+
为了找出包含一个“w”的名字,使用“[ww]”匹配小写或大写的“w”:
mysql> select * from pet where name regexp [ww];
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| claws | gwen | cat | m | 1994-03-17 | null |
| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |
| whistler | gwen | bird | null | 1997-12-09 | null |
+----------+-------+---------+------+------------+------------+
charindex另一种写法:
代码如下 复制代码
use mydatabase
select * from table1 where charindex('c',username)>0
这种方法理论上比上一种方法多了一个判断语句,即>0, 但这个判断过程是最快的, 我相信80%以上的运算都是花在查找字符串及其它的运算上, 所以运用charindex函数也没什么大不了。用这种方法也有好处, 那就是对%,|等在不能直接用like 查找到的字符中可以直接在这charindex中运用, 如下:
代码如下 复制代码
use mydatabase
select * from table1 where charindex('%',username)>0
大家还可以写成:
代码如下 复制代码
use mydatabase
select * from table1 where charindex(char(37),username)>0
ascii的字符即为%
sql like 的特殊字符
对于其他的特殊字符:'^', '-', ']' 因为它们本身在包含在 '[]' 中使用,所以需要用另外的方式来转义,于是就引入了 like 中的 escape 子句,另外值得注意的是:escape 可以转义所有的特殊字符。
代码如下 复制代码
select 1 where '^abcde' like '!^abcde' escape '!'
select 1 where '-abcde' like '!-abcde' escape '!'
select 1 where ']abcde' like '!]abcde' escape '!'
select 1 where '%abcde' like '%abcde' escape ''
select 1 where '%abcde' like '!%abcde' escape '!'
select 1 where '%abcde' like '#%abcde' escape '#'
select 1 where '%abcde' like '@%abcde' escape '@'
select 1 where '[abcde' like '![abcde' escape '!'
select 1 where ']abcde' like '!]abcde' escape '!'
看出规律了吧,就是用 escape 后面紧跟着的字符来做转义字符。 escape 后面的字符相当于 c 语言字符串中的转义字符 ''。
最后,看一个更加复杂的匹配
代码如下 复制代码
select 1 where '[^a-z]abcde' like '[^a-z]%' escape ''
