佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Oracle 兩個(gè)逗號(hào)分割的字符串,獲取交集、差集(sql實(shí)現(xiàn)過程解析)

Oracle 兩個(gè)逗號(hào)分割的字符串,獲取交集、差集(sql實(shí)現(xiàn)過程解析)

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 服務(wù)外包 Linux服務(wù)器 鐵路電話系統(tǒng) 百度競(jìng)價(jià)排名 呼叫中心市場(chǎng)需求 地方門戶網(wǎng)站 AI電銷

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

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

一、最終實(shí)現(xiàn)的sql語(yǔ)句

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
*/

二、實(shí)現(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ù),表示起始位置,從第幾個(gè)字符開始正則表達(dá)式匹配(默認(rèn)為1)
__occurrence:可選參數(shù),標(biāo)識(shí)第幾個(gè)匹配組,默認(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ù),表示起始位置,從第幾個(gè)字符開始正則表達(dá)式匹配(默認(rèn)為1)
__occurrence:可選參數(shù),標(biāo)識(shí)第幾個(gè)匹配組,默認(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單獨(dú)用,返回多行結(jié)果

select rownum from dual connect by rownum  5;

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

(2)一般通過start with . . . connect by . . .子句來實(shí)現(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ù)用法,可知下面語(yǔ)句可以把字符串“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個(gè)結(jié)果中使用集合運(yùn)算符(UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集)進(jìn)行最終處理。

總結(jié)

以上所述是小編給大家介紹的Oracle 兩個(gè)逗號(hào)分割的字符串,獲取交集、差集的sql實(shí)現(xiàn)過程解析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • Oracle通過正則表達(dá)式分割字符串 REGEXP_SUBSTR的代碼詳解
  • Oracle對(duì)兩個(gè)數(shù)據(jù)表交集的查詢

標(biāo)簽:黃山 蘭州 湘潭 銅川 仙桃 湖南 崇左 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle 兩個(gè)逗號(hào)分割的字符串,獲取交集、差集(sql實(shí)現(xiàn)過程解析)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    西乌珠穆沁旗| 木兰县| 偏关县| 鄂州市| 綦江县| 新和县| 常宁市| 泸州市| 郧西县| 乾安县| 庆城县| 莱西市| 都匀市| 五原县| 凤山县| 永福县| 清水县| 柏乡县| 阳原县| 车致| 韶山市| 甘洛县| 积石山| 临潭县| 东乌| 堆龙德庆县| 新民市| 呼伦贝尔市| 绥棱县| 兰州市| 海南省| 万载县| 搜索| 贵溪市| 高邑县| 忻城县| 大兴区| 云霄县| 佛山市| 昔阳县| 当涂县|