教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 sql server数据库模糊查询语句的例子

sql server数据库模糊查询语句的例子

发布时间:2016-05-08   编辑:jiaochengji.com
本文介绍了sql server数据库中进行模糊查询的语句,sql数据库中模糊查询的方法,有需要的朋友参考下。

一,在sql数据库中,确切匹配:
 

复制代码 代码示例:
select * from hs_user where id=123

二,sql数据库,模糊查询
 

复制代码 代码示例:
select * from hs_user where id like '%123%'

%为通配符
通配符:(like用于字符串,,,,,如果要对数字进行操作用in...in (200,230))

通配符 描述 示例
% 包含零个或更多字符的任意字符串。
where title like '%computer%' 将查找处于书名任意位置的包含单词 computer 的所有书名。
_(下划线) 任何单个字符。 where au_fname like '_ean' 将查找以 ean 结尾的所有 4 个字母的名字(dean、sean 等)。
[ ] 指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。 where au_lname like '[c-p]arsen' 将查找以arsen 结尾且以介于 c 与 p 之间的任何单个字符开始的作者姓氏,例如,carsen、larsen、karsen 等。
[^] 不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。 where au_lname like 'de[^l]%' 将查找以 de 开始且其后的字母不为 l 的所有作者的姓氏。
将通配符作为文字使用

可以将通配符模式匹配字符串用作文字字符串,方法是将通配符放在括号中。
下表显示了使用 like 关键字和 [ ] 通配符的示例。
 

符号 含义
like '5[%]' 5%
like '[_]n' _n
like '[a-cdf]' a、b、c、d 或 f
like '[-acdf]' -、a、c、d 或 f
like '[ [ ]' [
like ']' ]
like 'abc[_]d%' abc_d 和 abc_de
like 'abc[def]' abcd、abce 和 abcf
 

使用 escape 子句的模式匹配

可搜索包含一个或多个特殊通配符的字符串。例如,customers 数据库中的 discounts 表可能存储含百分号 (%) 的折扣值。若要搜索作为字符而不是通配符的百分号,必须提供 escape 关键字和转义符。例如,一个样本数据库包含名为 comment 的列,该列含文本 30%。若要搜索在 comment 列中的任何位置包含字符串 30% 的任何行,请指定由 where comment like '%30!%%' escape '!' 组成的 where 子句。如果不指定 escape 和转义符,sql server 将返回所有含字符串 30 的行。

怎么在 pubs 数据库 titles 表的 notes 列中搜索字符串"50% off when 100 or more copies are purchased":
 

复制代码 代码示例:
select notes from titles where notes like '50%% off when 100 or more copies are purchased' escape '%'

阐述escape 的作用:

1.使用   escape   关键字定义转义符。在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。例如,要搜索在任意位置包含字符串   5%   的字符串,请使用:   
 

复制代码 代码示例:
where   columna   like   '%5/%%'   escape   '/'

但是在mysql中好像不能使用"\"。

2.escape   'escape_character'   
允许在字符串中搜索通配符而不是将其作为通配符使用。escape_character   是放在通配符前表示此特殊用途的字符。
 

复制代码 代码示例:
select   *
from   finances
where   description   like   'gs_'   escape   's'
go
 

说明:
比如,要搜索一个字符串     "g_"     ,如果直接     like     "g_",那么   "_"的作用就是通配符,而不是字符,结果,我们会查到比如     "ga","gb","gc",而不是我们需要的   "g_".
用     like   'gs_'   escape   's'     's'表示特殊用法标志

3.

复制代码 代码示例:
create   table   a   (name   varchar(10))
go
insert   into   a   select   '11%22'
union   all   select   '11%33'
union   all   select   '12%33'
go
select   *   from   a     where   name   like   '%/%33'   escape   '/'   --指定用'/'符号来说明跟在其后面的通配符字符为普能字符。(第二个%是字符不是通配符来的)
go
drop   table   a

结果为:
name               
----------   
11%33
12%33

总结:
 

%:匹配零个及多个任意字符; _:与任意单字符匹配; []:匹配一个范围; [^]:排除一个范围
symbol meaning
like '5[%]' 5%
like '[_]n' _n
like '[a-cdf]' a, b, c, d, or f
like '[-acdf]' -, a, c, d, or f
like '[[]' [
like ']' ]
like 'abc[_]d%' abc_d and abc_de
like 'abc[def]' abcd, abce, and abcf
like '[^1-9]' 0
like '[^1-9b-z]' 0, a
 

对于字符串中出现的特殊字符:'%','[','[]', '_' 可以使用 '[]' 把它们包含起来,这样在匹配模式(pattern)中,它们就被当作普通字符对待了。

1. 用 like '[[]' 匹配特殊字符 '['
 

复制代码 代码示例:
select 1 where '[abcde' like '[[]%'

2. 用 like ']' 匹配特殊字符 ']'
 

复制代码 代码示例:
select 1 where ']abcde' like ']%'

3. 用 like '[[]]' 匹配特殊字符 '[]'
 

复制代码 代码示例:
select 1 where '[]abcde' like '[[]]%%'

4. 用 like '[_]' 匹配特殊字符 '_'
 

复制代码 代码示例:
select 1 where '_abcde' like '[_]%'

5. 用 like '[%]' 匹配特殊字符 '%'
 

复制代码 代码示例:
select 1 where 'abc%de' like 'abc[%]de'


对于其他的特殊字符:'^', '-', ']' 因为它们本身在包含在 '[]' 中使用,所以需要用另外的方式来转义,于是就引入了 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 '\'

使用escape   关键字定义转义符。
在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。

您可能感兴趣的文章:
sql模糊查询实例详解
mysql语句带参数模糊查询匹配问题
数据库查询语言(1)
php的mssql扩展SQL查询中文字段名的解决方法
分享:Mysql更新字段中部分数据的方法
Python数据库API(DB API)
有关 mysql 慢查询日志
设计高效合理的MySQL查询语句的建议
sql server数据库模糊查询语句的例子
php 简单内容查询代码 利用sql like模糊查询

关键词: sql通配符  模糊查询  通配符   
[关闭]
~ ~