日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Oracle?REGEXP_LIKE模糊查詢用法例子_oracle

作者:頑張ろう! ? 更新時(shí)間: 2022-12-14 編程語(yǔ)言

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%';

查詢結(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、多個(gè)OR條件時(shí),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、檢索以某個(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模糊查詢中的regexp_like嵌套子查詢用法

oracle模糊查詢中的regexp_like嵌套子查詢用法

regexp_like一般用于模糊查詢某一列時(shí)包含多個(gè)查詢條件

需求1:在用戶表中查詢出賬號(hào)包含650000和230000的用戶。

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:在另一張表中查詢出所需條件(查詢條件為另一個(gè)表的結(jié)果集),并在用戶表中以該條件模糊查詢對(duì)應(yīng)的用戶信息。

即在sys_org表中查出類型為1的orgid并以此結(jié)果在sys_user表中查詢出對(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)行查詢會(huì)報(bào)錯(cuò):“單行子查詢返回多行”

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、查詢條件并已完成,用 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é)

原文鏈接:https://blog.csdn.net/m0_37823115/article/details/124965854

欄目分類
最近更新