
本文将介绍Oracle的两个逗号分隔的字符串,以获取sql实现过程分析的交集和区别,并介绍操作方法:
Oracle的两个字段值是用逗号分隔的字符串. 例如,字段A的值为“ 1、2、3、5”,字段B的值为“ 2”. 我想获取两个字段的交集(相同的值)2,并获取两个字段之间的差(差值)1,3,5.
首先oracle 找出两个字符串的交集,最后的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; /*结果: 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; /*结果: 1 3 5 */
实施过程中使用的第二项功能使用说明
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 t7
from 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','[^,]+','*') t5
from 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)SQL的层次查询通常是通过以.. connect by ...子句开头的方式实现的.
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; /*结果: 1 广东 1 广东 2 广州 12 广东广州 3 深圳 13 广东深圳 */
三,总结
通过上面函数的使用,我们可以看到以下语句可以将字符串“ 1,2,3,5”转换为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
然后使用集合运算符(UNION / UNION ALL联合oracle 找出两个字符串的交集,INTERSECT相交,MINUS差集)对2个结果进行最终处理.
此处为您介绍了Oracle两个逗号分隔的字符串,分析了获取相异之处的sql实现过程,感谢您对c.lanmit.com的懒惰学习编程的关注.
本文地址:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shumachanpin/article-172472-1.html
还是三千亿
怎么就不能科学一点地去想想失足妇女合法化呢
另外两个送给了我最重要的两个人