增加postgresql中的列值
Incrementing the column value in postgresql
我有一个数据如下图
table_id store_ref store_type
100 store_a 1
101 store_a 1
102 store_a 1
我正在尝试复制同一个 table 中的记录,并更改 table_id 列值。 table id 列值应该自动 increment.Below 是预期的输出
输出:
table_id store_ref store_type
100 store_a 1
101 store_a 1
102 store_a 1
103 store_a 1
104 store_a 1
105 store_a 1
据推测,table_id
是一个 serial
列,因此它是自动设置的。那么你只需要做:
insert into t (store_ref, store_type)
select store_ref, store_type
from t
order by id;
如果由于某种原因id
不是serial
(我真的想不出一个好的理由),你可以计算它:
insert into t (id, store_ref, store_type)
select (max(id) over () + row_number() over (order by id)) as id,
store_ref, store_type
from t;
我有一个数据如下图
table_id store_ref store_type
100 store_a 1
101 store_a 1
102 store_a 1
我正在尝试复制同一个 table 中的记录,并更改 table_id 列值。 table id 列值应该自动 increment.Below 是预期的输出
输出:
table_id store_ref store_type
100 store_a 1
101 store_a 1
102 store_a 1
103 store_a 1
104 store_a 1
105 store_a 1
据推测,table_id
是一个 serial
列,因此它是自动设置的。那么你只需要做:
insert into t (store_ref, store_type)
select store_ref, store_type
from t
order by id;
如果由于某种原因id
不是serial
(我真的想不出一个好的理由),你可以计算它:
insert into t (id, store_ref, store_type)
select (max(id) over () + row_number() over (order by id)) as id,
store_ref, store_type
from t;