最近公司系统在做改造,数据库产品也换了,由Oracle数据库换成postgresql 数据库。
在开发的过程中,同事遇到一问题,就是有张表有个字段的数据存储格式是
A|B|C 这种形式, 现在要专业这种形式 钻石|铂金|黄金。
A对应的是钻石
B对应的是铂金
C对应的是黄金
刚开始以为要自定义一个函数,实现这样一个转换功能,偶然在qq群看到 pg有 regexp_split_to_table 函数。于是看是做实验测试:
--建表
drop table if exists test1;
drop table if exists test2;
create table test1(tier_code varchar(100));
create table test2(tier_code varchar(100),tier_name varchar(100));
--测试数据
insert into test1(tier_code) values ('A|B');
insert into test1(tier_code) values('A|B|C');
insert into test1(tier_code) values('A|B|C|E');
insert into test2(tier_code,tier_name) values('A','钻石');
insert into test2(tier_code,tier_name) values('B','铂金');
insert into test2(tier_code,tier_name) values('C','黄金');
insert into test2(tier_code,tier_name) values('E','黑钻');