Oracle 两个逗号分割的字符串,获取交集、差集的sql实现过程解析
Oracle数据库的两个字段值为逗号分割的字符串,例如:字段A值为“1,2,3,5”,字段B为“2”。
想获取两个字段的交集(相同值)2,获取两个字段的差集(差异值)1,3,5。
一、最终实现的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 '' 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 '' 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','','') 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)一般通过start with . . . connect by . . .子句来实现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; /*结果:
1 广东 \1 \广东
2 广州 \1\2 \广东\广州
3 深圳 \1\3 \广东\深圳
*/
三、总结
由上面函数用法,可知下面语句可以把字符串“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
然后在2个结果中使用集合运算符(UNION/UNION ALL 并集,INTERSECT 交集,MINUS 差集)进行最终处理。
Oracle 两个逗号分割的字符串,获取交集、差集的sql实现过程解析的更多相关文章
- oracle 两个逗号分割的字符串 如何判断是否其中有相同值
比如字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cc,dd' 没有相同值 字段A: 'ab,cd,ef,gh'字段B: 'aa,bb,cd,dd' 有相同值cd 1.CREATE OR ...
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列 前台传来的字符串:'589,321' SELECT*FROM TAB_A T1 WHERE T1.CODE IN ( SEL ...
- Java将一段逗号分割的字符串转换成一个数组
String 类:String 类代表字符串.Java 程序中的所有字符串字面值都作为此类的实例实现.字符串是常量,它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串.因为 String 对象是 ...
- java基础面试题:如何把一段逗号分割的字符串转换成一个数组? String s = "a" +"b" + "c" + "d";生成几个对象?
package com.swift; public class Douhao_String_Test { public static void main(String[] args) { /* * 如 ...
- mysql根据逗号分割的字符串去关联查询另外一个表的数据
1.说明 在做显示数据的时候,一个字段会存那种逗号分割的字符串,那如何去根据逗号分割字符串去查询另一个表的数据呢? 首先我们查看一下需要显示的数据 select * from company wher ...
- MYSQL: sql中某一个字段内容为用逗号分割的字符串转换成多条数据
场景: 表名:testsuer id name 1 小红,小李,李红,小法 要结果值为: 1 小红 1 小李 1 李红 1 小法 MYSQL函数解释 ...
- multiselect获取选中的多个下拉项的值(逗号分割的字符串)
/*传入下拉select标签*/ function get_selected(mslt_employees) { var emplo =mslt_employees.multiselect(" ...
- oracle实用sql之将逗号分割的字符串分割多个列
select regexp_substr('a,b,c,','[^,]+',1,rownum) from dual connect by rownum<=length(regexp_replac ...
- Oracle中通过逗号分割字符串并转换成多行
通过逗号对字符串字段进行分割,并返回多行,通过使用regexp_substr()函数实现. SQL示例: select regexp_substr(q.nums, '[^,]+', 1, rownum ...
随机推荐
- Office安装时报错1907的解决方法
大家在装office时可能遇到过1907错误,字体无法注册 的问题,百度后者是采用什么四种方法,我四种方法都试过了没有用,也有人说缺什么日本字体,我也试过了没有用,做为有强迫证的我都重做系统,重下载o ...
- [20191218]降序索引疑问4.txt
[20191218]降序索引疑问4.txt --//前几天优化一个项目,我发现许多表里面有有隐含字段,一般开发很少建立函数索引.我自己检查发现里面存在大量的降序索引.--//我感觉有点奇怪,为什么开发 ...
- 管网平差的python程序
在市政给水管网当中,管网平差的目的是在已知节点流量.管段长度的情况下,求得各管段流量和对应的经济管径.本科生学习阶段了解并掌握管网平差原理及方法是必不可少的环节. 在下面的程序当中,将利用哈代克罗斯法 ...
- Mysql 的异常:The last packet successfully received from the server was 90 milliseconds ago. The last packet sent successfully to the server was 43,603,303 milliseconds ago. is longer than the server con
调试一个程序, 调试到一半, 下班回家, 程序卡在了某一行, 第二天早上回来一看, 发现了异常: Wed Sep :: GMT+: WARN: Establishing SSL connection ...
- vs2017 C# ActiveX浏览器插件 创建 发布 C# windows窗体控件库(.NET Framework)注意事项
vs2017需要安装插 插件下载地址:https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.Micro ...
- (转)LSTM神经网络介绍
原文链接:http://www.atyun.com/16821.html 扩展阅读: https://machinelearningmastery.com/time-series-prediction ...
- How to: Map a Persistent Class to a Database View Which Has No Key Field如何:映射持久化类到无主键数据库视图
With XAF, you can build new applications from scratch or maintain existing databases. The How to: Ge ...
- vue应用调试工具 vue-devtools安装
方法一:chrome直接访问下面地址下载安装:(需要翻墙) https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejigli ...
- HTTP与FILE协议的区别
File协议 file协议(本地文件传输协议)主要是用来访问本地计算机的文件,一般用Windows的资源管理器直接打开进行读取一个HTML文件时,默认会使用file协议 基本格式是: file:/// ...
- Viewpager+Fragment 跳转Activity报错android.os.TransactionTooLargeException: data parcel size xxxxx bytes
Viewpager + Fragment 跳转Activity报错android.os.TransactionTooLargeException: data parcel size xxxxx byt ...