Image I have the following table: (in reality there are more columns)
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
:
user_id | product_id
---------|------------
1| 1
1| 2
1| 3
2| 2
2| 3
3| 1
3| 2
3| 3
2
Answers
As already demonstrated by @Adrian Klaver, that’s a simple query with a constant in place of the
user_id
:demo at db<>fiddle
There is, but
information_schema
to add those columns into your query, but that’s pretty uglyjsonb
trick are quite costly and ugly, so it’s better to make sure benefits of this justify that priceYou can use auto-mapping Postgres does when converting records to and from
jsonb
:to_jsonb()
grabs entire rows from the table and maps them tojsonb
.jsonb_set()
changesuser_id
to a3
.jsonb_populate_record
maps the updatedjsonb
back to aclone_test
record.().*
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.