【问题标题】:Extract multiple values from a string for each id从每个 id 的字符串中提取多个值
【发布时间】:2021-05-19 07:50:04
【问题描述】:

我想从每个 id 的字符串列中提取匹配项。我怎样才能做到这一点?

+--------+---------------------------------------+
|   id   |                 text                  |
+--------+---------------------------------------+
| fsaf12 | Other Questions,Missing Document      |
| sfas11 | Others,Missing Address,Missing Name   |
+--------+---------------------------------------+

期望的输出:

+--------+------------------+
|   id   |    extracted     |
+--------+------------------+
| fsaf12 | Other Questions  |
| fsaf12 | Missing Document |
| sfas11 | Others           |
| sfas11 | Missing Address  |
| sfas11 | Missing Name     |
+--------+------------------+

这里是样本数据的查询:FIDDLE

【问题讨论】:

  • 我不知道为什么这个问题被否决了。这看起来很简单,但对于新的来说有点棘手。

标签: regex postgresql split


【解决方案1】:

您可以使用regexp_split_to_table 来满足您的要求,如下所示:

WITH t1 AS (
    SELECT 'fsaf12' AS id, 'Other Questions,Missing Document' AS text UNION ALL
    SELECT 'sfas11', 'Others,Missing Address,Missing Name'
)
SELECT id, regexp_split_to_table(text,',')
FROM t1

输出


| id        | extracted             |
|-----------|-----------------------|
| fsaf12    | Other Questions       |
| fsaf12    | Missing Document      |
| sfas11    | Others                |
| sfas11    | Missing Address       |
| sfas11    | Missing Name          |

DEMO

【讨论】:

    【解决方案2】:

    Postgres 根本不是我的强项,但基于此older 帖子,您可以尝试使用unnest()。我添加了一个TRIM() 以在拆分后删除可能的栏杆空间:

    SELECT id, TRIM(unnest(string_to_array(text, ','))) as "extracted" FROM t1;
    

    或者,如果你想使用regexp_split_to_table()

    SELECT id, regexp_split_to_table(text, '\s*,\s*') as "extracted" FROM t1;
    

    这里我们匹配 0+ 个空白字符、一个文字逗号和 0+ 个空白字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多