a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 數(shù)據(jù)庫 > Oracle > 正文

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

2020-07-26 13:26:38
字體:
供稿:網(wǎng)友

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

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

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

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

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

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

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

二、實(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ù),表示起始位置,從第幾個字符開始正則表達(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 t7from 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','[^,]+','*') t5from 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, '/') namepathfrom (select 1 id, '廣東' name, 0 pid from dualunion select 2 id, '廣州' name , 1 pid from dualunion select 3 id, '深圳' name , 1 pid from dual) start with pid = 0connect 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) idfrom (select '1,2,3,5' id from dual)connect by rownum <= length(regexp_replace(id, '[^,]+')) +1

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

總結(jié)

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲一级免费观看 | 日韩欧美精品区 | 欧美极品视频 | 精品探花| 成人一区二区三区在线观看 | 国产精品亚洲一区二区三区在线 | 国产日韩一区二区三区 | 国产一区二区三区免费 | 中文字幕久久久 | 日本色站 | 成人欧美一区二区三区在线播放 | 激情一区二区三区 | 国产免费成人在线视频 | 色呦呦网站 | www.伊人.com | 久久综合久 | 美女张开腿视频网站免费 | 日本在线网 | 国产精品成人在线 | 狠狠爱www人成狠狠爱综合网 | 91性高湖久久久久久久久_久久99 | 色综合久久88色综合天天 | 国产精品极品美女在线观看免费 | 黄色在线观看免费 | 国产精品一区久久 | 亚洲精品一区二区在线观看 | 中文字幕一区二区三区乱码在线 | 亚洲一二三四在线 | 久久精品电影 | 免费一级淫片aaa片毛片a级 | 久久一区二区三区精品 | 99re| 综合一区二区三区 | 成人毛片在线免费看 | 久久视频免费在线 | 精品久久久久久亚洲精品 | 亚洲一区中文字幕永久在线 | 国产一区二区 | 在线播放国产一区二区三区 | 性高湖久久久久久久久aaaaa | 欧美伊人影院 |