PostgreSQL中如何将bigint列数据复制到bigint[]列?
解决方案:将单个bigint值复制到bigint数组列
你之前的SQL语句存在两个问题:
- 赋值方向颠倒了,应该是把
customerId的值赋给customerIds,而非反过来 - 没有将单个
bigint值转换为bigint[]数组类型
PostgreSQL中可以使用ARRAY[]构造器将单个值包装成单元素数组,执行以下语句即可完成需求:
UPDATE table_name SET customerIds = ARRAY[customerId];
如果需要明确指定数组类型(避免潜在的类型歧义),可以加上类型转换:
UPDATE table_name SET customerIds = ARRAY[customerId]::bigint[];
若只想更新customerId不为空的行,可添加WHERE条件过滤:
UPDATE table_name SET customerIds = ARRAY[customerId] WHERE customerId IS NOT NULL;
内容的提问来源于stack exchange,提问作者rosed
相关产品推荐
相关产品推荐

