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

问题描述 投票:0回答:1

我编写了一个存储过程来进行一些数据匹配。完成后,我更新 MATCH_DETAILS 列,表明它已与 ID 匹配,但它也可能与其他 ID 匹配。

我想编写 SQL 来根据我在 MATCH_DETAILS 列中指定的 ID 列表从表中读取数据。

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

因此,我的 SQL 将查找文本“Possible QSID:”,然后在这之后提取 ID。

我尝试编写一些 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...

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

干杯

oracle subquery substring
1个回答
1
投票

这是一种选择;阅读代码中的注释:

样本数据:

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 numeric 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>
© www.soinside.com 2019 - 2024. All rights reserved.