Oracle?REGEXP_LIKE模糊查詢(xún)用法例子
1、函數(shù)介紹
REGEXP_LIKE 函數(shù)在功能上與 LIKE 函數(shù)非常相似。 然而,雖然 LIKE 允許簡(jiǎn)單的字符串匹配搜索,但 REGEXP_LIKE 函數(shù)非常強(qiáng)大,因?yàn)槌俗址ヅ渌阉髦?,它還可以使用正則表達(dá)式。 REGEXP_LIKE 可以按如下方式使用。
REGEXP_LIKE ( string , pattern [, match] )
REGEXP_LIKE ( 檢索對(duì)象 , 檢索字符 [, 檢索參數(shù)] )
' 'i':不區(qū)分大小寫(xiě)
'c':區(qū)分大小寫(xiě)
'n':將換行符與“n”句點(diǎn) (.) 匹配
'm':將搜索目標(biāo)視為多行,并將“^”和“$”匹配到每行的開(kāi)頭和結(jié)尾。
進(jìn)行測(cè)試之前先創(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ū)分大小寫(xiě)
2-1、LIKE 函數(shù)
SELECT message_val AS message_val_like FROM test_table_regexp_like WHERE LOWER(message_val) LIKE '%need%';
查詢(xún)結(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');
查詢(xún)結(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、多個(gè)OR條件時(shí),LIKE函數(shù)就不能滿(mǎn)足需求了。
-- 包含 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');
查詢(xún)結(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、檢索以某個(gè)字符串開(kāi)頭的數(shù)據(jù)
-- 以 "I" 開(kā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、檢索以某個(gè)字符串結(jié)尾的數(shù)據(jù)
-- 以 "I" 開(kā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.
補(bǔ)充:oracle模糊查詢(xún)中的regexp_like嵌套子查詢(xún)用法
oracle模糊查詢(xún)中的regexp_like嵌套子查詢(xún)用法
regexp_like一般用于模糊查詢(xún)某一列時(shí)包含多個(gè)查詢(xún)條件
需求1:在用戶(hù)表中查詢(xún)出賬號(hào)包含650000和230000的用戶(hù)。
select * from sys_user where regexp_like(account,'650000|230000')
以上的寫(xiě)法等同于下面的寫(xiě)法:
select * from sys_user where account like '%650000%' or account like'%230000%'
需求2:在另一張表中查詢(xún)出所需條件(查詢(xún)條件為另一個(gè)表的結(jié)果集),并在用戶(hù)表中以該條件模糊查詢(xún)對(duì)應(yīng)的用戶(hù)信息。
即在sys_org表中查出類(lèi)型為1的orgid并以此結(jié)果在sys_user表中查詢(xún)出對(duì)應(yīng)的賬號(hào)信息。
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é)果集進(jìn)行查詢(xún)會(huì)報(bào)錯(cuò):“單行子查詢(xún)返回多行”
1、將結(jié)果集顯示成一列。所用函數(shù):wm_concat(列名)
注:wm_concat(列名),該函數(shù)可以把列值以","號(hào)分隔起來(lái),并顯示成一行,即“行轉(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、查詢(xún)條件并已完成,用 regexp_like查詢(xún)出所需信息即可
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模糊查詢(xún)用法的文章就介紹到這了,更多相關(guān)Oracle REGEXP_LIKE模糊查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Oracle中分析函數(shù)over()的用法及說(shuō)明
這篇文章主要介紹了Oracle中分析函數(shù)over()的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Oracle監(jiān)聽(tīng)口令及監(jiān)聽(tīng)器安全詳解
這篇文章主要介紹了Oracle監(jiān)聽(tīng)口令及監(jiān)聽(tīng)器安全的解決方法,需要的朋友可以參考下2014-07-07WINDOWS下使用DOS命令行連接oracle數(shù)據(jù)庫(kù)
本文講述了通過(guò)windows下的DOS命令連接oracle數(shù)據(jù)庫(kù)并進(jìn)行簡(jiǎn)單操作的方法2018-03-03oracle創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)三步走
這篇文章主要介紹了oracle如何創(chuàng)建數(shù)據(jù)庫(kù)和用戶(hù),以及每一步所涉及到的代碼,需要的朋友可以參考下2015-08-08Oracle數(shù)據(jù)庫(kù)中ORDER BY排序和查詢(xún)按IN條件的順序輸出
這篇文章主要介紹了Oracle數(shù)據(jù)庫(kù)中ORDER BY排序和查詢(xún)按IN條件的順序輸出的方法,其中ORDER BY的排序結(jié)果需要注意其是否穩(wěn)定,需要的朋友可以參考下2015-11-11oracle 存儲(chǔ)過(guò)程返回 結(jié)果集 table形式的案例
這篇文章主要介紹了oracle 存儲(chǔ)過(guò)程返回 結(jié)果集 table形式的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01