Oracle如何拆分字段并且关联上对应ID?

xibmg 2019-01-15 08:05:53
有一个表,存的数据大概是:
ID DESC
1 A,B,C,DE
2 M,LD,D,C
3 H,MM,SD,WE
如何拆分成:
ID DESC
1 A
1 B
1 C
1 DE
2 M
2 LD
2 D
2 C
3 H
3 MM
3 SD
3 WE
网上的资料大多是用regexp_substr 加上connect by 实现单个字段拆分,但是此处是要求拆分后的结果带上对应的ID序号,因为需要关联ID来获取对应拆分后的多行结果。
求大神赐教!
...全文
476 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 5 楼 xibmg 的回复:
[quote=引用 4 楼 wangfutai91的回复:]with tmp as (select id, regexp_substr(des, '[^,]+', 1, level) new_des from desc_info connect by level <= regexp_count(des, ',') + 1 and prior rowid = rowid and prior dbms_random.value is not null) select * from tmp where id = 1; 1 A 1 B 1 C 1 DE 这个:[align=left] 不清楚是什么
desc_info表有很大的数据量,这个查询效率非常差[/quote] 这是递归 性能肯定差。 而且他是把所有结果 生成的cet表 再select。 你可以把 where id=1 加在 拆分字符的那个sql上 直接查询。
xibmg 2019-01-18
  • 打赏
  • 举报
回复
引用 4 楼 wangfutai91的回复:
with tmp as
(select id, regexp_substr(des, '[^,]+', 1, level) new_des
from desc_info
connect by level <= regexp_count(des, ',') + 1
and prior rowid = rowid
and prior dbms_random.value is not null)
select * from tmp where id = 1;


1 A
1 B
1 C
1 DE
这个:[align=left] 不清楚是什么
desc_info表有很大的数据量,这个查询效率非常差
wangfutai91 2019-01-16
  • 打赏
  • 举报
回复
select id, regexp_substr(des, '[^,]+', 1, level) new_des
from desc_info
connect by level <= regexp_count(des, ',') + 1
and prior rowid = rowid
and prior dbms_random.value is not null;
结果:
1 A
1 B
1 C
1 DE
2 M
2 LD
2 D
2 C
3 H
3 MM
3 SD
3 WE
nayi_224 2019-01-16
  • 打赏
  • 举报
回复
and id = prior id
wangfutai91 2019-01-16
  • 打赏
  • 举报
回复
with tmp as
(select id, regexp_substr(des, '[^,]+', 1, level) new_des
from desc_info
connect by level <= regexp_count(des, ',') + 1
and prior rowid = rowid
and prior dbms_random.value is not null)
select * from tmp where id = 1;


1 A
1 B
1 C
1 DE
这个:[align=left] 不清楚是什么
xibmg 2019-01-16
  • 打赏
  • 举报
回复
引用 2 楼 wangfutai91 的回复:
select id, regexp_substr(des, '[^,]+', 1, level) new_des
from desc_info
connect by level <= regexp_count(des, ',') + 1
and prior rowid = rowid
and prior dbms_random.value is not null;
结果:
1 A
1 B
1 C
1 DE
2 M
2 LD
2 D
2 C
3 H
3 MM
3 SD
3 WE


我要查询 id =1 的数据拆分结果,
id desc
1 A
1 B
1 C
1 DE


应该怎么写?

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧