Oracle -根据存储在列中的ID从表中选择行

yjghlzjz  于 2023-08-03  发布在  Oracle
关注(0)|答案(1)|浏览(112)

我写了一个存储过程来做一些数据匹配。当它完成时,我更新一个MATCH_DETAILS列,声明它已经与一个ID匹配,但它也可以与其他ID匹配。
我想根据我在MATCH_DETAILS列中指定的ID列表编写SQL来从表中读取。

Matched with multiple QSIDs, updating with QSID: 6030066992. Possible QSIDs: 6030066992,1777,

字符集
因此,我的SQL将查找文本“Possible QSID:“然后把身份证拿出来
我试着写了一点SQL,但它没有做我想要的,它只是返回一个字符串,但我需要个人ID的做整体搜索:

select SUBSTR(cd.match_details,
                  INSTR(cd.match_details, 'Possible QSIDs: ', 1) + 16,
                  length(cd.match_details) - 1)
      from scv_client_details cd
     where cd.match_details like 'Matched with multiple QSIDs%'


这只是返回这个:

6030066992,1777,
3602,3600,
etc...


理想情况下,我希望将它们作为数字返回,每行一个:

6030066992
1777
3602
3600
etc...


有人知道这里需要什么吗?
干杯

uttx8gqw

uttx8gqw1#

这里有一个选择;读取代码中的注解:
样本数据:

SQL> with scv_client_details  (match_details) as
  2    (select 'Matched with multiple QSIDs, updating with QSID: 6030066992. Possible QSIDs: 6030066992,1777,'
  3     from dual
  4     union all
  5     select 'Matched with multiple QSIDs. Yet another example; Possible QSIDs: 12345,6789'
  6     from dual
  7    ),

字符集
查询从这里开始:

8  temp as
  9    -- return strings that follow "Possible QSIDs:"
 10    (select trim(replace(substr(match_Details, instr(match_details, 'Possible QSIDs:')),
 11                    'Possible QSIDs:',
 12                    null
 13                   )) val
 14     from scv_client_details
 15    ),
 16  temp2 as
 17    -- split previously fetched comma-separated strings to rows. Avoid duplicate rows!
 18    (select regexp_substr(val, '[^,]+', 1, column_value) val
 19     from temp cross join
 20       table(cast(multiset(select level from dual
 21                           connect by level <= regexp_count(val, ',') + 1
 22                          ) as sys.odcinumberlist))
 23    )
 24  -- finally, result is numberic part of previously fetched VAL
 25  select regexp_substr(val, '\d+') result
 26  from temp2
 27  where regexp_like(val, '\d+');


测试结果:

RESULT
--------------------------------------------------------------------------------
6030066992
1777
12345
6789

SQL>

相关问题