Elitmiar
Elitmiar

Reputation: 36839

Updating a row in postgres from another row

I have two tables one called details and the other called c_details. Both tables are exact the same except for different table names

No I a row with data in both of these tables

Is it possible to update the row in details with the row in c_details

Eg.

update details SET (Select * from c_details)?

Upvotes: 5

Views: 6712

Answers (3)

ss.
ss.

Reputation: 672

Use this sql query:

INSERT INTO details SELECT * FROM c_details

Upvotes: 0

New Alexandria
New Alexandria

Reputation: 7324

If you need to UPDATE FROM SELECT and SET a field based on an aggregate function (MIN) then then SQL should read:

UPDATE
  details
SET
  name = (
    SELECT 
      MIN(c.create_at) 
    FROM
      c_details AS c
    WHERE
      c.type = 4
  )
WHERE
  c.id = details.id;

Upvotes: 5

Kouber Saparev
Kouber Saparev

Reputation: 8105

You have to describe explicitly the list of columns to be updated, as well as the key to match columns between the two tables.

The syntax for updating one table from another table is described in detail in the UPDATE chapter of the PostgreSQL documentation.

UPDATE
  details
SET
  name = c.name,
  description = c.description
FROM
  c_details AS c
WHERE
  c.id=details.id;

Upvotes: 9

Related Questions