首页数据库Oracle 两个逗号分割的字符串,获取交集、差集(sql实现过程解析)

Oracle 两个逗号分割的字符串,获取交集、差集(sql实现过程解析)

时间2024-02-28 16:32:03发布访客分类数据库浏览980
导读:收集整理的这篇文章主要介绍了Oracle 两个逗号分割的字符串,获取交集、差集(sql实现过程解析 ,觉得挺不错的,现在分享给大家,也给大家做个参考。 oracle数据库的两个字段值为逗...
收集整理的这篇文章主要介绍了Oracle 两个逗号分割的字符串,获取交集、差集(sql实现过程解析),觉得挺不错的,现在分享给大家,也给大家做个参考。

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实现过程解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

@H_505_126@ 您可能感兴趣的文章:
    @H_777_130@Oracle通过正则表达式分割字符串 REGEXP_SUBSTR的代码详解
  • Oracle对两个数据表交集的查询

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Oracle 两个逗号分割的字符串,获取交集、差集(sql实现过程解析)
本文地址: https://pptw.com/jishu/631669.html
Win Oracle 监听文件配置参考代码实例 oracle 使用rownum的三种分页方式

游客 回复需填写必要信息