Oracle?REGEXP_LIKE模糊查詢用法例子
1、函數(shù)介紹
REGEXP_LIKE 函數(shù)在功能上與 LIKE 函數(shù)非常相似。 然而,雖然 LIKE 允許簡單的字符串匹配搜索,但 REGEXP_LIKE 函數(shù)非常強大,因為除了字符串匹配搜索之外,它還可以使用正則表達式。 REGEXP_LIKE 可以按如下方式使用。
REGEXP_LIKE ( string , pattern [, match] )
REGEXP_LIKE ( 檢索對象 , 檢索字符 [, 檢索參數(shù)] )
' 'i':不區(qū)分大小寫
'c':區(qū)分大小寫
'n':將換行符與“n”句點 (.) 匹配
'm':將搜索目標(biāo)視為多行,并將“^”和“$”匹配到每行的開頭和結(jié)尾。
進行測試之前先創(chuàng)建表
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、使用此函數(shù)不區(qū)分大小寫
2-1、LIKE 函數(shù)
SELECT message_val AS message_val_like FROM test_table_regexp_like WHERE LOWER(message_val) LIKE '%need%';
查詢結(jié)果:
message_val
--------------------------------------------------
Data Discrepancy needs to be fixed.
I am in desperate NEED of money.
I really NeeD you forever.
2-2、REGEXP_LIKE 函數(shù)
SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, 'need', 'i');
查詢結(jié)果
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函數(shù)就不能滿足需求了。
-- 包含 am 或者 NeeD 的數(shù)據(jù) -- ”|“ 作用 = or SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, 'am|NeeD');
查詢結(jié)果
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)');
檢索結(jié)果
message_val_regexp_like
--------------------------------------------------
I really NeeD you forever.
3-3、檢索以某個字符串開頭的數(shù)據(jù)
-- 以 "I" 開頭,中間有 "you" 的字符串。 -- "^" 字符串首位 -- ".*" 任意字符串 SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, '^I.*you.*');
檢索結(jié)果
message_val_regexp_like
--------------------------------------------------
I really NeeD you forever.
3-4、檢索以某個字符串結(jié)尾的數(shù)據(jù)
-- 以 "I" 開頭、以 "money." 結(jié)尾的字符串 -- "^" :字符串首位 -- "$" :字符串末尾 SELECT message_val AS message_val_regexp_like FROM test_table_regexp_like WHERE REGEXP_LIKE(message_val, '^I.*money.$');
檢索結(jié)果
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:在另一張表中查詢出所需條件(查詢條件為另一個表的結(jié)果集),并在用戶表中以該條件模糊查詢對應(yīng)的用戶信息。
即在sys_org表中查出類型為1的orgid并以此結(jié)果在sys_user表中查詢出對應(yīng)的賬號信息。
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 )))
解決思路:
若是以此結(jié)果集進行查詢會報錯:“單行子查詢返回多行”
1、將結(jié)果集顯示成一列。所用函數(shù):wm_concat(列名)
注:wm_concat(列名),該函數(shù)可以把列值以","號分隔起來,并顯示成一行,即“行轉(zhuǎn)列”
select wm_concat(orgid) from (select orgid from sys_org where orgtype = '1' order by orgid )
2、將結(jié)果集用 | 分隔
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 )))
總結(jié)
到此這篇關(guān)于Oracle REGEXP_LIKE模糊查詢用法的文章就介紹到這了,更多相關(guān)Oracle REGEXP_LIKE模糊查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Oracle監(jiān)聽口令及監(jiān)聽器安全詳解
這篇文章主要介紹了Oracle監(jiān)聽口令及監(jiān)聽器安全的解決方法,需要的朋友可以參考下2014-07-07WINDOWS下使用DOS命令行連接oracle數(shù)據(jù)庫
本文講述了通過windows下的DOS命令連接oracle數(shù)據(jù)庫并進行簡單操作的方法2018-03-03oracle創(chuàng)建一個數(shù)據(jù)庫三步走
這篇文章主要介紹了oracle如何創(chuàng)建數(shù)據(jù)庫和用戶,以及每一步所涉及到的代碼,需要的朋友可以參考下2015-08-08Oracle數(shù)據(jù)庫中ORDER BY排序和查詢按IN條件的順序輸出
這篇文章主要介紹了Oracle數(shù)據(jù)庫中ORDER BY排序和查詢按IN條件的順序輸出的方法,其中ORDER BY的排序結(jié)果需要注意其是否穩(wěn)定,需要的朋友可以參考下2015-11-11oracle 存儲過程返回 結(jié)果集 table形式的案例
這篇文章主要介紹了oracle 存儲過程返回 結(jié)果集 table形式的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01