如何从列表中删除字符
How to delete character from list
我有排,
示例:1,2,3,5,9,7
-> 不在 (3,7) 中
(这个字符需要删除 -> 结果 select 1,2,5,9
。
怎么办?
例如:
drop table test.table_4;
create table test.table_4 (
id integer,
list_id text
);
insert into test.table_4 values(1,'1,2,3,5,9,7');
insert into test.table_4 values(2,'1,2,3,5');
insert into test.table_4 values(3,'7,9');
insert into test.table_4 values(5,'1,2');
insert into test.table_4 values(9,'1');
insert into test.table_4 values(7,'5,7,9');
查询:
select list_id from test.table_4 where id not in (3,7) --return 4 row
id list_id
1. 1 '1,2,3,5,9,7'
2. 2 '1,2,3,5'
3. 5 '1,2'
4. 9 '1'
如何删除第 1 行和第 2 行中的 3 和 7?
id
1. 1 '1,2,5,9'
2. 2 '1,2,5'
3. 5 '1,2'
4. 9 '1'
您可以使用以下语句来更新所有记录。在下面的示例中,第一个语句将删除找到的任何 ,7
。然后执行下一条语句以查找字符串前面具有 7
的任何字符串。
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',7', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '7', '')
如果您还想删除所有出现的 3
则执行以下语句:
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',3', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '3', '')
但是,将您需要搜索、处理等的值存储在字符串中是一个糟糕的设计。
以下应处理字符串开头、字符串结尾或中间任意位置的 3 或 7。它还确保 31
中的 3 和 17
中的 7 不会被替换:
select
list_id,
regexp_replace(list_id, '(^[37],|,[37](,)|,[37]$)', '', 'g')
from test.table_4
where id not in (3,7)
解释:
^[37],
匹配 3 或 7 后跟字符串开头的逗号。这应该用任何东西代替。
,[37](,)
匹配字符串中间的 ,3 或 ,7。这需要用一个逗号替换,逗号由它周围的方括号捕获。
[37]$
匹配字符串末尾以逗号开头的 3 或 7。这应该用任何东西代替。
</code> 用于替换字符串 - 这是上面第二种情况的 <code>,
,对于情况 1 和 3 为空。
您可以使用 regexp_replace 获得预期的输出:
select id, regexp_replace(list_id,'3,|,7', '','g')
from table_4
where id not in (3,7)
输出:
id regexp_replace
1 1,2,5,9
2 1,2,5
5 1,2
9 1
我有排,
示例:1,2,3,5,9,7
-> 不在 (3,7) 中
(这个字符需要删除 -> 结果 select 1,2,5,9
。
怎么办?
例如:
drop table test.table_4;
create table test.table_4 (
id integer,
list_id text
);
insert into test.table_4 values(1,'1,2,3,5,9,7');
insert into test.table_4 values(2,'1,2,3,5');
insert into test.table_4 values(3,'7,9');
insert into test.table_4 values(5,'1,2');
insert into test.table_4 values(9,'1');
insert into test.table_4 values(7,'5,7,9');
查询:
select list_id from test.table_4 where id not in (3,7) --return 4 row
id list_id
1. 1 '1,2,3,5,9,7'
2. 2 '1,2,3,5'
3. 5 '1,2'
4. 9 '1'
如何删除第 1 行和第 2 行中的 3 和 7?
id
1. 1 '1,2,5,9'
2. 2 '1,2,5'
3. 5 '1,2'
4. 9 '1'
您可以使用以下语句来更新所有记录。在下面的示例中,第一个语句将删除找到的任何 ,7
。然后执行下一条语句以查找字符串前面具有 7
的任何字符串。
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',7', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '7', '')
如果您还想删除所有出现的 3
则执行以下语句:
UPDATE test.table_4 SET list_id = REPLACE(list_id, ',3', '')
UPDATE test.table_4 SET list_id = REPLACE(list_id, '3', '')
但是,将您需要搜索、处理等的值存储在字符串中是一个糟糕的设计。
以下应处理字符串开头、字符串结尾或中间任意位置的 3 或 7。它还确保 31
中的 3 和 17
中的 7 不会被替换:
select
list_id,
regexp_replace(list_id, '(^[37],|,[37](,)|,[37]$)', '', 'g')
from test.table_4
where id not in (3,7)
解释:
^[37],
匹配 3 或 7 后跟字符串开头的逗号。这应该用任何东西代替。
,[37](,)
匹配字符串中间的 ,3 或 ,7。这需要用一个逗号替换,逗号由它周围的方括号捕获。
[37]$
匹配字符串末尾以逗号开头的 3 或 7。这应该用任何东西代替。
</code> 用于替换字符串 - 这是上面第二种情况的 <code>,
,对于情况 1 和 3 为空。
您可以使用 regexp_replace 获得预期的输出:
select id, regexp_replace(list_id,'3,|,7', '','g')
from table_4
where id not in (3,7)
输出:
id regexp_replace
1 1,2,5,9
2 1,2,5
5 1,2
9 1