網站首頁 編程語言 正文
1、函數介紹
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.');
2、使用此函數不區分大小寫
2-1、LIKE 函數
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.
2-2、REGEXP_LIKE 函數
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.
3、多條件
3-1、多個OR條件時,LIKE函數就不能滿足需求了。
-- 包含 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.
3-2、AND :既有A又有B
-- 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.
3-3、檢索以某個字符串開頭的數據
-- 以 "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.
3-4、檢索以某個字符串結尾的數據
-- 以 "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嵌套子查詢用法
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 )))
總結
原文鏈接:https://blog.csdn.net/m0_37823115/article/details/124965854
相關推薦
- 2022-07-02 C#并行編程之數據并行Tasks.Parallel類_C#教程
- 2022-08-31 Python無法用requests獲取網頁源碼的解決方法_python
- 2023-12-07 WARNING: Access control is not enabled for the dat
- 2022-05-17 configure: error: Building GCC requires GMP 4.2+,
- 2022-12-24 詳解C#?parallel中并行計算的四種寫法總結_C#教程
- 2022-01-18 ES6新語法(解構賦值、模板字符串)
- 2022-07-07 Python使用captcha庫制作帶參數輸入驗證碼案例_python
- 2022-06-07 docker-compose配置并部署redis服務的實現_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支