17,377
社区成员
发帖
与我相关
我的任务
分享
select * from employees_copy t where t.first_name not like '%o%';
select * from employees_copy t where t.first_name like '%[^o]%';
select * from employees_copy t where regexp_like(t.first_name,'^[m-o]');--以m,n或o开头
select * from employees_copy t where regexp_like(t.first_name,'^[^m-o]');--不以m,n或o开头
select * from employees_copy t where regexp_like(t.first_name,'[m-o]$');--以m,n或o结尾
select * from employees_copy t where regexp_like(t.first_name,'[^m-o]$');--不以m,n或o结尾
关于表达式中的两个符号查到的资料是这样解释的:
POSIX 正则表达式由标准的元字符(metacharacters)所构成:
'^' 匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。
'$' 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 '\n' 或 '\r'。
根据测试,我的理解是'^'放在[ ]里面表示的是否定,即表示需查询的结果不包含方括号中的这些字符,若放在[ ]的前面则表示开头,即需查询的结果要以方括号中的这些字符开头,’$‘若放在[ ]中则与其他字母一样都是表示范围,若放在[ ]的后面则表示结尾,即需查询的结果要以方括号中的这些字符结束,但两者不能同时放在开头和结尾,类似于这样:select * from employees_copy t where regexp_like(t.first_name,'^[m-o]$');
现在的问题有两个:上面的理解正确吗?在Oracle中若要写带有[ ] 表示范围的模糊查询语句是否只能使用正则表达式或是放弃方括号的使用直接用not like?大神们,求赐教啊
with tmp as
(
select 'aboc' first_name from dual union all
select 'abc' first_name from dual
)
select *
from tmp
where regexp_like(first_name,'[,o]+') ;
with tmp as
(
select 'aboc' first_name from dual union all
select 'abc' first_name from dual
)
select *
from tmp
where not regexp_like(first_name,'[,o]+') ;
--查询姓名里包含字母o的人
select * from employees_copy where regexp_like(first_name,'[,o]+');
--查询姓名里不包含字母o的人
select * from employees_copy where not regexp_like(first_name,'[,o]+');