业务场景:
当前数据库表中有一列存着多个值的编码字段,通过特殊字符’,'隔开.
需要与维表中的编码关联取出名称并且拆分为多行展示。
建立测试表如下:
select * from ods.my_table t
select * from ods.my_table1 t1
实现方法:
运用string_to_array和any函数实现
select * from ods.my_table t
left join ods.my_table1 t1
on t1.code=any(string_to_array(t.code,’,’));
运行结果如下:
string_to_array函数简介:
函数定义:将字符串转换成数组
转换后的格式为:
转换语句:
select t.*,string_to_array(t.code,’,’) from ods.my_table t
第一个参数为需要转换的字符串
第二个参数为字符串中每个数组的值的分隔符