国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

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

2020-07-26 13:26:38
字體:
來源:轉載
供稿:網友

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

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

一、最終實現的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;/*結果: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;/*結果:135*/

二、實現過程用到的函數用法說明

1、regexp_substr

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

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

/*結果:
1    2    3    5    2    2    3
*/

2、regexp_replace

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

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

/*結果:
1,2,3,4    1,4,4,5    ,,,    ,,,    *,*,*,*
*/

3、connect by

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

select rownum from dual connect by rownum < 5;

/*結果:
1
2
3
4
*/

(2)一般通過start with . . . connect by . . .子句來實現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;

/*結果:
1    廣東    /1    /廣東
2    廣州    /1/2    /廣東/廣州
3    深圳    /1/3    /廣東/深圳
*/

三、總結

由上面函數用法,可知下面語句可以把字符串“1,2,3,5”轉換為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個結果中使用集合運算符(UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集)進行最終處理。

總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛隆县| 平度市| 崇礼县| 海口市| 汤阴县| 江安县| 宝应县| 广丰县| 林芝县| 皋兰县| 弥勒县| 巴南区| 嘉黎县| 古蔺县| 泽库县| 绥江县| 许昌市| 辉南县| 唐山市| 乐昌市| 永胜县| 榕江县| 忻城县| 海阳市| 临西县| 丰都县| 四会市| 泾阳县| 大石桥市| 仪陇县| 柳州市| 高雄市| 布拖县| 荆州市| 灵丘县| 边坝县| 屏山县| 襄垣县| 南开区| 灌云县| 松滋市|