Oracle 兩個逗號分割的字符串,獲取交集、差集(sql實現(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
正則表達(dá)式分割字符串,函數(shù)格式如下:
function regexp_substr(strstr, pattern [,position] [,occurrence] [,modifier] [subexpression]) __srcstr:需要進(jìn)行正則處理的字符串 __pattern:進(jìn)行匹配的正則表達(dá)式 __position:可選參數(shù),表示起始位置,從第幾個字符開始正則表達(dá)式匹配(默認(rèn)為1) __occurrence:可選參數(shù),標(biāo)識第幾個匹配組,默認(rèn)為1 __modifier:可選參數(shù),表示模式('i'不區(qū)分大小寫進(jìn)行檢索;'c'區(qū)分大小寫進(jìn)行檢索。默認(rèn)為'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
通過正則表達(dá)式來進(jìn)行匹配替換,函數(shù)格式如下:
function regexp_substr(srcstr, pattern [,replacestr] [,position] [,occurrence] [,modifier]) __srcstr:需要進(jìn)行正則處理的字符串 __pattern:進(jìn)行匹配的正則表達(dá)式 __replacestr:可選參數(shù),替換的字符串,默認(rèn)為空字符串 __position:可選參數(shù),表示起始位置,從第幾個字符開始正則表達(dá)式匹配(默認(rèn)為1) __occurrence:可選參數(shù),標(biāo)識第幾個匹配組,默認(rèn)為1 __modifier:可選參數(shù),表示模式('i'不區(qū)分大小寫進(jìn)行檢索;'c'區(qū)分大小寫進(jìn)行檢索。默認(rèn)為'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 差集)進(jìn)行最終處理。
總結(jié)
以上所述是小編給大家介紹的Oracle 兩個逗號分割的字符串,獲取交集、差集的sql實現(xiàn)過程解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Oracle根據(jù)逗號拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說明
在做系統(tǒng)時經(jīng)常會遇到在一個字段中,用逗號或其他符號分隔存儲多個信息,下面這篇文章主要給大家介紹了關(guān)于Oracle根據(jù)逗號拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說明,需要的朋友可以參考下2023-04-04Oracle數(shù)據(jù)庫把多行轉(zhuǎn)一列逗號分割兩種方法
Oracle將行轉(zhuǎn)換為列是指將關(guān)系型數(shù)據(jù)庫中的行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù)的操作,這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫把多行轉(zhuǎn)一列逗號分割兩種方法的相關(guān)資料,需要的朋友可以參考下2024-07-07Oracle的用戶、角色及權(quán)限相關(guān)操作
這篇文章主要介紹了Oracle的用戶、角色及權(quán)限相關(guān)操作,需要的朋友可以參考下2017-07-07Oracle undo_management參數(shù)不一致錯誤
因RAC的undo_management參數(shù)不一致導(dǎo)致Oracle數(shù)據(jù)庫mount報ORA-01105 ORA-01606錯誤,本文就這個問題2013-11-11Redhat 6.2 下 oracle 11g R2的安裝詳解
這篇文章主要介紹了Redhat 6.2 下 oracle 11g R2的安裝詳解,需要的朋友可以參考下2016-07-07