gpt4 book ai didi

postgresql - Postgres : Create duplicates of existing rows, 改变一个值?

转载 作者:行者123 更新时间:2023-12-05 03:12:02 25 4
gpt4 key购买 nike

我在 Postgres 9.4 中工作。我有一个看起来像这样的表:

     Column      │         Type         │                             Modifiers
─────────────────┼──────────────────────┼───────────────────────
id │ integer │ not null default
total_list_size │ integer │ not null
date │ date │ not null
pct_id │ character varying(3) │

我想获取 date='2015-09-01' 的所有值,并创建日期为 2015-10-01 的相同新条目。

我怎样才能最好地做到这一点?

我可以使用 SELECT * from mytable WHERE date='2015-09-01' 获取要复制的值列表,但我不确定之后要做什么。

最佳答案

如果列 idserial 那么

INSERT INTO mytable (total_list_size, date, pct_id)
SELECT total_list_size, '2015-10-01', pct_id
FROM mytable
WHERE date = '2015-09-01';

否则,如果您希望复制 id:

INSERT INTO mytable (id, total_list_size, date, pct_id)
SELECT id, total_list_size, '2015-10-01', pct_id
FROM mytable
WHERE date = '2015-09-01';

关于postgresql - Postgres : Create duplicates of existing rows, 改变一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35222236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com