溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Oracle如何實現(xiàn)兩個逗號分割的字符串以及獲取交集、差集

發(fā)布時間:2021-05-19 13:52:32 來源:億速云 閱讀:721 作者:小新 欄目:數(shù)據(jù)庫

這篇文章主要介紹了Oracle如何實現(xiàn)兩個逗號分割的字符串以及獲取交集、差集,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Oracle數(shù)據(jù)庫的兩個字段值為逗號分割的字符串,例如:字段A值為“1,2,3,5”,字段B為“2”。

想獲取兩個字段的交集(相同值)2,獲取兩個字段的差集(差異值)1,3,5。

一、最終實現(xiàn)的sql語句

1、獲取交集(相同值):

select regexp_substr(id, '[^,]+', 1, rownum) id
from (select '1,2,3,5' id from dual)
connect by rownum <= length(regexp_replace(id, '[^,]+')) +1
intersect -- 取交集
select regexp_substr(id, '[^,]+', 1, rownum) id
from (select '2' id from dual)
connect by rownum <= length(regexp_replace(id, '[^,]+')) +1;
/*結(jié)果:
2
*/

2、獲取差集(差異值):

select regexp_substr(id, '[^,]+', 1, rownum) id
from (select '1,2,3,5' id from dual)
connect by rownum <= length(regexp_replace(id, '[^,]+')) +1
minus --取差集
select regexp_substr(id, '[^,]+', 1, rownum) id
from (select '2' id from dual)
connect by rownum <= length(regexp_replace(id, '[^,]+')) +1;
/*結(jié)果:
1
3
5
*/

二、實現(xiàn)過程用到的函數(shù)用法說明

1、regexp_substr

正則表達式分割字符串,函數(shù)格式如下:

function regexp_substr(strstr, pattern [,position] [,occurrence] [,modifier] [subexpression])
__srcstr:需要進行正則處理的字符串
__pattern:進行匹配的正則表達式
__position:可選參數(shù),表示起始位置,從第幾個字符開始正則表達式匹配(默認為1)
__occurrence:可選參數(shù),標識第幾個匹配組,默認為1
__modifier:可選參數(shù),表示模式('i'不區(qū)分大小寫進行檢索;'c'區(qū)分大小寫進行檢索。默認為'c'。)

使用例子:

select 
regexp_substr('1,2,3,5','[^,]+') AS t1, 
regexp_substr('1,2,3,5','[^,]+',1,2) AS t2,
regexp_substr('1,2,3,5','[^,]+',1,3) AS t3,
regexp_substr('1,2,3,5','[^,]+',1,4) AS t4,
regexp_substr('1,2,3,5','[^,]+',2) AS t5,
regexp_substr('1,2,3,5','[^,]+',2,1) AS t6,
regexp_substr('1,2,3,5','[^,]+',2,2) AS t7
from dual;

/*結(jié)果:
1    2    3    5    2    2    3
*/

2、regexp_replace

通過正則表達式來進行匹配替換,函數(shù)格式如下:

function regexp_substr(srcstr, pattern [,replacestr] [,position] [,occurrence] [,modifier])
__srcstr:需要進行正則處理的字符串
__pattern:進行匹配的正則表達式
__replacestr:可選參數(shù),替換的字符串,默認為空字符串
__position:可選參數(shù),表示起始位置,從第幾個字符開始正則表達式匹配(默認為1)
__occurrence:可選參數(shù),標識第幾個匹配組,默認為1
__modifier:可選參數(shù),表示模式('i'不區(qū)分大小寫進行檢索;'c'區(qū)分大小寫進行檢索。默認為'c'。)

使用例子:

select 
regexp_replace('1,2,3,5','5','4') t1,
regexp_replace('1,2,3,5','2|3',4) t2,
regexp_replace('1,2,3,5','[^,]+') t3,
regexp_replace('1,2,3,5','[^,]+','') t4,
regexp_replace('1,2,3,5','[^,]+','*') t5
from dual;

/*結(jié)果:
1,2,3,4    1,4,4,5    ,,,    ,,,    *,*,*,*
*/

3、connect by

(1)connect by單獨用,返回多行結(jié)果

select rownum from dual connect by rownum < 5;

/*結(jié)果:
1
2
3
4
*/

(2)一般通過start with . . . connect by . . .子句來實現(xiàn)SQL的層次查詢

select 
id,
name,
sys_connect_by_path(id,'\') idpath,
sys_connect_by_path(name, '\') namepath
from (
select 1 id, '廣東' name, 0 pid from dual
union 
select 2 id, '廣州' name , 1 pid from dual
union 
select 3 id, '深圳' name , 1 pid from dual
) 
start with pid = 0
connect by prior id = pid;

/*結(jié)果:
1    廣東    \1    \廣東
2    廣州    \1\2    \廣東\廣州
3    深圳    \1\3    \廣東\深圳
*/

三、總結(jié)

由上面函數(shù)用法,可知下面語句可以把字符串“1,2,3,5”轉(zhuǎn)換為4行記錄

select regexp_substr(id, '[^,]+', 1, rownum) id
from (select '1,2,3,5' id from dual)
connect by rownum <= length(regexp_replace(id, '[^,]+')) +1

然后在2個結(jié)果中使用集合運算符(UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集)進行最終處理。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Oracle如何實現(xiàn)兩個逗號分割的字符串以及獲取交集、差集”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI