Oracle 一条数据拆成多条数据

需求如下图所示,第一条数据的nums字段为1,2,3则需要拆成3条数据,4,5需要拆成两条,5,6需要拆成两条

select '1,2,3' nums, '张三' names,'1' sex from dual
  union all
  select '4,5' nums, '李四' names,'1' sex from dual
  union all
  select '5,6' nums, '王五' names,'1' sex from dual
image.png

oracle代码实现:

select distinct * from (
select regexp_substr(q.nums, '[^,]+', 1, Level,'i') order_num, names,q.sex
  from (
  select '1,2,3' nums, '张三' names,'1' sex from dual
  union all
  select '4,5' nums, '李四' names,'1' sex from dual
  union all
  select '5,6' nums, '王五' names,'1' sex from dual
  ) q
connect by Level <= LENGTH(q.nums) - LENGTH(REGEXP_REPLACE(q.nums, ',', '')) + 1) 
order by order_num;

结果显示:


image.png

你可能感兴趣的:(Oracle 一条数据拆成多条数据)