开发者

Updating a row in postgres from another row

开发者 https://www.devze.com 2022-12-10 12:41 出处:网络
I have two tables one called details and the other called c_details开发者_运维知识库. Both tables are exact the same except for different table names

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)?


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;


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;


Use this sql query:

INSERT INTO details SELECT * FROM c_details
0

精彩评论

暂无评论...
验证码 换一张
取 消