我正在尝试将逗号分隔的字符串转换为整数数组(integer []),以在Where子句中使用。
我尝试过cast,::Int
但都没有成功。感谢您的建议
示例
Table A | Table B
ID | Set_id
2 | 14,16,17
1 | 15,19,20
3 | 21
我的查询:
Select *
from Table a, table b
where a.id in b.set_id
我正在尝试将逗号分隔的字符串转换为整数数组(integer []),以在Where子句中使用。
我尝试过cast,::Int
但都没有成功。感谢您的建议
示例
Table A | Table B
ID | Set_id
2 | 14,16,17
1 | 15,19,20
3 | 21
我的查询:
Select *
from Table a, table b
where a.id in b.set_id
如果您想将字符串用作连接条件,您需要将其转换为适当的整数数组。
Select *
from Table a
join table b on a.id = any(string_to_array(b.set_id, ',')::int[]);
但是,一个更好的解决方案是正确地对表进行规范化(或者至少将这些ID存储在整数数组中,而不是varchar列中)。Select * from Table_a a, table_b b
where a.id = any(regexp_split_to_array(b.set_id,',')::int[]);
使用unnest(string_to_array(b.set_id,',') :: int [])选择* from Table_a a,table_b b where a.id in ;