REGEXP_LIKE 函数在功能上与 LIKE 函数非常相似。 然而,虽然 LIKE 允许简单的字符串匹配搜索,但 REGEXP_LIKE 函数非常强大,因为除了字符串匹配搜索之外,它还可以使用正则表达式。 REGEXP_LIKE 可以按如下方式使用。
REGEXP_LIKE ( string , pattern [, match] )
REGEXP_LIKE ( 检索对象 , 检索字符 [, 检索参数] )
' 'i':不区分大小写
'c':区分大小写
'n':将换行符与“n”句点 (.) 匹配
'm':将搜索目标视为多行,并将“^”和“$”匹配到每行的开头和结尾。
进行测试之前先创建表
CREATE TABLE test_table_regexp_like ( message_val VARCHAR2(50) NOT NULL ); INSERT INTO test_table_regexp_like VALUES ("Data Discrepancy needs to be fixed."); INSERT INTO test_table_regexp_like VALUES ("I am in desperate NEED of money."); INSERT INTO test_table_regexp_like VALUES ("I really NeeD you forever.");
SELECT message_val AS message_val_like FROM test_table_regexp_like WHERE LOWER(message_val) LIKE "%need%";
查询结果:
message_val
--------------------------------------------------
Data Discrepancy needs to be fixed.
I am in desperate NEED of money.
I really NeeD you forever.
SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, "need", "i");
查询结果
message_val_regexp_like
--------------------------------------------------
DATA Discrepancy needs TO be fixed.
I am IN desperate NEED OF money.
I really NeeD you forever.
-- 包含 am 或者 NeeD 的数据 -- ”|“ 作用 = or SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, "am|NeeD");
查询结果
message_val_regexp_like
--------------------------------------------------
I am IN desperate NEED OF money.
I really NeeD you forever.
-- AND 条件 ("really" 和 "you" 都包含的) -- "()|()" = AND SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, "(really)|(you)");
检索结果
message_val_regexp_like
--------------------------------------------------
I really NeeD you forever.
-- 以 "I" 开头,中间有 "you" 的字符串。 -- "^" 字符串首位 -- ".*" 任意字符串 SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, "^I.*you.*");
检索结果
message_val_regexp_like
--------------------------------------------------
I really NeeD you forever.
-- 以 "I" 开头、以 "money." 结尾的字符串 -- "^" :字符串首位 -- "$" :字符串末尾 SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, "^I.*money.$");
检索结果
message_val_regexp_like
--------------------------------------------------
I am IN desperate NEED OF money.
oracle模糊查询中的regexp_like嵌套子查询用法
regexp_like一般用于模糊查询某一列时包含多个查询条件
需求1:在用户表中查询出账号包含650000和230000的用户。
select * from sys_user where regexp_like(account,"650000|230000")
以上的写法等同于下面的写法:
select * from sys_user where account like "%650000%" or account like"%230000%"
需求2:在另一张表中查询出所需条件(查询条件为另一个表的结果集),并在用户表中以该条件模糊查询对应的用户信息。
即在sys_org表中查出类型为1的orgid并以此结果在sys_user表中查询出对应的账号信息。
select fullname,account from sys_user where REGEXP_LIKE (account,(select replace(wm_concat(orgid),",","|") from (select orgid from sys_org where orgtype = "1" order by orgid )))
解决思路:
若是以此结果集进行查询会报错:“单行子查询返回多行”
1、将结果集显示成一列。所用函数:wm_concat(列名)
注:wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,即“行转列”
select wm_concat(orgid) from (select orgid from sys_org where orgtype = "1" order by orgid )
2、将结果集用 | 分隔
select replace(wm_concat(orgid),",","|") from (select orgid from sys_org where orgtype = "1" order by orgid )
3、查询条件并已完成,用 regexp_like查询出所需信息即可
select fullname,account from sys_user where REGEXP_LIKE (account,(select replace(wm_concat(orgid),",","|") from (select orgid from sys_org where orgtype = "1" order by orgid )))
到此这篇关于Oracle REGEXP_LIKE模糊查询用法的文章就介绍到这了,更多相关Oracle REGEXP_LIKE模糊查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!