PostgreSQL: Clone rows but replace one column

Image I have the following table: (in reality there are more columns)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> user_id | product_id
---------|------------
1| 1
1| 2
1| 3
2| 2
2| 3
</code>
<code> user_id | product_id ---------|------------ 1| 1 1| 2 1| 3 2| 2 2| 3 </code>
 user_id | product_id
---------|------------
        1|           1
        1|           2
        1|           3
        2|           2
        2|           3

Basically, I need to copy all rows with user_id = 1 but change it to user_id = 3:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> user_id | product_id
---------|------------
1| 1
1| 2
1| 3
2| 2
2| 3
3| 1
3| 2
3| 3
</code>
<code> user_id | product_id ---------|------------ 1| 1 1| 2 1| 3 2| 2 2| 3 3| 1 3| 2 3| 3 </code>
 user_id | product_id
---------|------------
        1|           1
        1|           2
        1|           3
        2|           2
        2|           3
        3|           1
        3|           2
        3|           3

1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create table clone_test( user_id integer, product_id integer);
insert into clone_test values (1,1), (1,2), (1,3), (2,2), (2,3);
insert into clone_test (select 3, product_id from clone_test where user_id =1);
select * from clone_test ;
user_id | product_id
---------+------------
1 | 1
1 | 2
1 | 3
2 | 2
2 | 3
3 | 1
3 | 2
3 | 3
</code>
<code>create table clone_test( user_id integer, product_id integer); insert into clone_test values (1,1), (1,2), (1,3), (2,2), (2,3); insert into clone_test (select 3, product_id from clone_test where user_id =1); select * from clone_test ; user_id | product_id ---------+------------ 1 | 1 1 | 2 1 | 3 2 | 2 2 | 3 3 | 1 3 | 2 3 | 3 </code>
create table clone_test( user_id integer, product_id integer);

insert into clone_test values (1,1), (1,2), (1,3), (2,2), (2,3);

insert into clone_test (select 3, product_id from clone_test where user_id =1);

select * from clone_test ;
 user_id | product_id 
---------+------------
       1 |          1
       1 |          2
       1 |          3
       2 |          2
       2 |          3
       3 |          1
       3 |          2
       3 |          3

4

copy all rows with user_id = 1 but change it to user_id = 3:

As already demonstrated by @Adrian Klaver, that’s a simple query with a constant in place of the user_id:
demo at db<>fiddle

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>insert into your_table
select 3 as user_id, product_id
from your_table
where user_id=1;
</code>
<code>insert into your_table select 3 as user_id, product_id from your_table where user_id=1; </code>
insert into your_table 
select 3 as user_id, product_id 
from your_table
where user_id=1;

Is there any way to do it without manually listing all other columns?

There is, but

  • modern IDEs can typically be configured to identify your db schema, so they can add those columns on their own, without you having to type all of them out
  • dynamic SQL can be used to query your information_schema to add those columns into your query, but that’s pretty ugly
  • both dynamic SQL and the jsonb trick are quite costly and ugly, so it’s better to make sure benefits of this justify that price

You can use auto-mapping Postgres does when converting records to and from jsonb:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>insert into clone_test
select (jsonb_populate_record( null::clone_test
,jsonb_set(to_jsonb(clone_test)
,'{user_id}'
,to_jsonb(3))
)
).*
from clone_test
where user_id=1;
</code>
<code>insert into clone_test select (jsonb_populate_record( null::clone_test ,jsonb_set(to_jsonb(clone_test) ,'{user_id}' ,to_jsonb(3)) ) ).* from clone_test where user_id=1; </code>
insert into clone_test 
select (jsonb_populate_record( null::clone_test
                              ,jsonb_set(to_jsonb(clone_test) 
                                         ,'{user_id}'
                                         ,to_jsonb(3))
                             )
       ).*
from clone_test
where user_id=1;
  1. to_jsonb() grabs entire rows from the table and maps them to jsonb.
  2. In each, jsonb_set() changes user_id to a 3.
  3. jsonb_populate_record maps the updated jsonb back to a clone_test record.
  4. ().* unpacks the record, separating the columns.

Doing all that just to avoid having to tab through auto-completion, type out or copy the column list, is an overkill.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật