我有一个表 Job
,其中有一列 Comment
包含以下数据:
-#AB1 #XY: https://xxxxxxxxxx/xxx/xxx/xxxxxxxx/12345/xxxxxxx
-#XY: https://xxxxxxx/xxx/xxx/xxxxxxxx/23456/xxxxx #AB2
-#XY: https://xxxxxxxxxx/xxx/xxx/xxxxxxxx/34567/xxxx
-#XY: https://xxxxxxxx/xxx/xxx/xxxxxx/45677/xxxxxxxx
因此,如果在评论栏中的 #XY 中,它应该给我链接中的数字 (https://xxxxxxxxxx/xxx/xxx/xxxxxxxx/12345/xxxxxxx
)。
如果列中同时有#XY 和#AB,它应该给我一个接近#AB 的数字。
我用过:
SELECT substring(comment FROM '[0-9]+') AS original_id,
FROM job
WHERE internal_comment LIKE '%AB%'
但这只给我列中的第一个数字。谁能帮我解决这个问题。
最佳答案
假设链接中除了突出显示的数字之外没有其他数字,这应该可以工作:
SELECT
/* match in any order */
CASE WHEN comment ~ '(#AB.*#XY)|(#XY.*#AB)'
/* match #AB{ any digits } and get only the number */
THEN (REGEXP_MATCHES(Comment, '(#AB)(\d+)'))[2]
/* find the digits in the link */
ELSE (REGEXP_MATCHES(comment, '\d+'))[1] END AS original_id
FROM job;
工作 fiddle here
关于sql - 从列中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52493317/