现在的位置: 首页 > 综合 > 正文

Select语句—包含通配符的查询

2012年06月02日 ⁄ 综合 ⁄ 共 1575字 ⁄ 字号 评论关闭
1、/*通配符_(下划线)*/
/*_
功能:代表一个字符
用法:只能用在where字句中,通常_与运算符like配合使用
*/
/*查询LastName中第三个字符是a的人*/
select LastName from Employees where LastName like '__a%';

/*查询LastName中第三个字符是a,并且LastName只有三个字符*/
select LastName from Employees where LastName like '__a';

2、/*通配符[]*/
/*[]用来限定单个字符界定于指定的范围或集合中*/

select City from Customers where City like '[A-E]%'
select City from Customers where City like '[MC]%'
/*查询电话号码 :第一位为2-4的电话号码*/
select Phone from Customers where Phone like '[2-4]%'
/*查询电话号码 :第一位为2或4的电话号码*/
select Phone from Customers where Phone like '[24]%'

/*查询城市 :第一字符为B,第二字符为A或E的城市*/
select City from Customers where City like 'B[AE]%'

/*重点注意:查询姓名 查得姓名完全是中文文字(不包含任何英文或数字)*/
select name from my where name not like '%[a-z]%' and name not like '%[0-9]%'

3、/*[^] 不在指定的范围或集合中 相当于“非[]”*/
/*如:
[^A-D] [^1-9]
[^ACD] [^135]
*/
/*第一个字符不包括A、B、C、D*/
select LastName from Employees where LastName like '[^A-D]%'
/*第一个字符不是A、C、D*/
select LastName from Employees where LastName like '[^ACD]%'

/*查询LastName中第三个字符是a,并且LastName只有三个字符*/
select LastName from Employees where LastName like '__a';

2、/*通配符[]*/
/*[]用来限定单个字符界定于指定的范围或集合中*/

select City from Customers where City like '[A-E]%'
select City from Customers where City like '[MC]%'
/*查询电话号码 :第一位为2-4的电话号码*/
select Phone from Customers where Phone like '[2-4]%'
/*查询电话号码 :第一位为2或4的电话号码*/
select Phone from Customers where Phone like '[24]%'

/*查询城市 :第一字符为B,第二字符为A或E的城市*/
select City from Customers where City like 'B[AE]%'

/*重点注意:查询姓名 查得姓名完全是中文文字(不包含任何英文或数字)*/
select name from my where name not like '%[a-z]%' and name not like '%[0-9]%'

3、/*[^] 不在指定的范围或集合中 相当于“非[]”*/
/*如:
[^A-D] [^1-9]
[^ACD] [^135]
*/
/*第一个字符不包括A、B、C、D*/
select LastName from Employees where LastName like '[^A-D]%'
/*第一个字符不是A、C、D*/
select LastName from Employees where LastName like '[^ACD]%'

抱歉!评论已关闭.