如何从 PostgreSQL 字符列中删除多个字符串

huangapple go评论145阅读模式
英文:

How to removing multiple strings from a PostgreSQL string column

问题

  1. UPDATE
  2. candidates
  3. SET
  4. "current_ids" = regexp_replace("current_ids", '|ABX1-003|ABX1-006|ABX1-007|', '|', 'g'),
  5. "total_ids" = regexp_replace("total_ids", '|ABX1-003|ABX1-006|ABX1-007|', '|', 'g')
  6. WHERE
  7. "current_ids" ~ '|ABX1-003|ABX1-006|ABX1-007|' OR
  8. "total_ids" ~ '|ABX1-003|ABX1-006|ABX1-007|';

这个 SQL 查询会从 "current_ids" 和 "total_ids" 列中删除指定的字符串,并在多个地方执行此操作。如果字符串出现在列中,它们都将被替换成单个的竖线字符 "|".

英文:

In my PSQL database, I have the following table structure and I would like to understand how I can remove multiple strings from the currentIdsand totalIds

  1. CREATE TABLE "public"."candidates" (
  2. "day" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
  3. "study_id" varchar(6) NOT NULL,
  4. "site_id" varchar(32),
  5. "status" varchar(32) NOT NULL,
  6. "total" int4 NOT NULL,
  7. "current" int4 NOT NULL,
  8. "referrer_token" varchar(255) NOT NULL DEFAULT 'NO_REFERRER_TOKEN'::character varying,
  9. "total_ids" text NOT NULL DEFAULT '|'::text,
  10. "current_ids" text NOT NULL DEFAULT '|'::text
  11. );

What I want to achieve is the following example.

I have for example:

  1. current_ids: '|ABX1-001|ABX1-002|ABX1-003|ABX1-004|ABX1-005|ABX1-006|'
  2. total_ids: '|ABX1-001|ABX1-002|ABX1-003|ABX1-004|ABX1-004|ABX1-006|ABX1-007|'

From those 2 columns, I want to remove the IDs: 'ABX1-003', 'ABX1-006', 'ABX1-007'

so the final result will be:

  1. current_ids: '|ABX1-001|ABX1-002|ABX1-004|ABX1-005|'
  2. total_ids: '|ABX1-001|ABX1-002|ABX1-004|ABX1-004|'

I tried so far something like this but didn't work

  1. UPDATE
  2. candidates
  3. SET
  4. "current_ids" = regexp_replace("current_ids", 'ABX1-003|ABX1-006|ABX1-007', '', 'g')
  5. WHERE
  6. "current_ids" in('ABX1-003', 'ABX1-006', 'ABX1-007')

答案1

得分: 0

以下查询将仅更新包含将被更改的字符串的行:

  1. WITH patterns(pattern) AS (
  2. VALUES ('(?<=\|)(ABX1-003|ABX1-006|ABX1-007)\|'))
  3. UPDATE candidates
  4. SET current_ids = regexp_replace(current_ids, patterns.pattern, '', 'g'),
  5. total_ids = regexp_replace(current_ids, patterns.pattern, '', 'g')
  6. FROM patterns
  7. WHERE candidates.current_ids ~ patterns.pattern
  8. OR candidates.total_ids ~ patterns.pattern;

一个单一的模式用于选择将受影响的行并驱动正则表达式替换。前瞻断言 (?<=\|) 确保每个替换匹配之前有一个 | 分隔符,而不会将 | 包括在匹配中;因此,只会找到完整字段匹配。

英文:

The following query will only update rows with strings that will be changed:

  1. WITH patterns(pattern) AS (
  2. VALUES (&#39;(?&lt;=\|)(ABX1-003|ABX1-006|ABX1-007)\|&#39;))
  3. UPDATE candidates
  4. SET current_ids = regexp_replace(current_ids, patterns.pattern, &#39;&#39;, &#39;g&#39;),
  5. total_ids = regexp_replace(current_ids, patterns.pattern, &#39;&#39;, &#39;g&#39;)
  6. FROM patterns
  7. WHERE candidates.current_ids ~ patterns.pattern
  8. OR candidates.total_ids ~ patterns.pattern;

A single pattern is used to both select the rows that will be affected and to drive the regular expression replacement. The lookbehind, (?&lt;=\|), insures that each replacement match is preceded by a | delimiter without consuming the | by the match; thus, only full field matches will be found.

huangapple
  • 本文由 发表于 2023年6月19日 23:21:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定