gpt4 book ai didi

string - 删除 PostgreSQL 表字符串中的子字符串

转载 作者:行者123 更新时间:2023-12-02 21:52:05 24 4
gpt4 key购买 nike

我在 PostgreSQL 数据库中有表“A”:

name     fullname
A A,I,A
B B,B,S,B
D D,D,S,E,D
E E,E,D,G,E
F F,F,G,F
G G,E,G,F,G,H,N,G

如何接收包含“正确名称”(全名)列的表“B”,其中每行中“名称”字段中没有匹配的子字符串:

name  correctname
A I
B S
D S,E
E D,G
F G
G E,F,H,N

数据示例:

CREATE TABLE test(name TEXT, fullname TEXT);

INSERT INTO test(name, fullname) VALUES('A','A,I,A');
INSERT INTO test(name, fullname) VALUES('B','B,B,S,B');
INSERT INTO test(name, fullname) VALUES('D','D,D,S,E,D');
INSERT INTO test(name, fullname) VALUES('E','E,E,D,G,E');
INSERT INTO test(name, fullname) VALUES('F','F,F,G,F');
INSERT INTO test(name, fullname) VALUES('G','G,E,G,F,G,H,N,G');

谢谢!

最佳答案

Solution :

SELECT 
name
,trim(
REGEXP_REPLACE(fullname,'(,|^)('||name||'(,|$))+',',','cg')
,',')
FROM data;

SELECT REPLACE(REPLACE(fullname, name||',', ''), ','||name, '') FROM test;

关于string - 删除 PostgreSQL 表字符串中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18400494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com