简体   繁体   English

如何仅更新 PostgreSQL 9.4 中另一个表的更改值?

[英]How can I update only changed values from another table in PostgreSQL 9.4?

I have this query but updates the whole region_tmp table, which I dont want to, I only need to update a row in region table if anything has changed.我有这个查询但更新了整个 region_tmp 表,我不想这样做,如果有任何更改,我只需要更新区域表中的一行。 I know I can specify which row with the regionid but am looking for something more general since I have a lot of records.我知道我可以用 regionid 指定哪一行,但我正在寻找更通用的东西,因为我有很多记录。

UPDATE REGION_TMP t SET
      REGIONID=R.REGIONID,
      REGIONDESCRIPTION=R.REGIONDESCRIPTION
FROM REGION R
WHERE R.REGIONID = T.REGIONID;

REGION TABLE:区域表:

REGIONID区域ID REGIONDESCRIPTION区域描述
1 1个 AMERICA美国
2 2个 EUROPE欧洲
3 3个 ASIA亚洲

REGION_TMP TABLE: REGION_TMP 表:

REGIONID区域ID REGIONDESCRIPTION区域描述
1 1个 AMERICA美国
2 2个 EUROPE欧洲
3 3个 AFRICA非洲

My desire output in REGION_TMP:我希望 REGION_TMP 中的 output:

REGIONID区域ID REGIONDESCRIPTION区域描述
1 1个 AMERICA美国
2 2个 EUROPE欧洲
3 3个 ASIA亚洲

My guess is that you need to create a duplicate of your REGION table, but you already have a REGION_TMP table, so you just want to update the values that do not match.我的猜测是您需要创建REGION表的副本,但您已经有一个REGION_TMP表,所以您只想更新不匹配的值。

In order to achieve that, you need to join the two tables inside the UPDATE statement and replace the values of REGION_TMP.REGIONDESCRIPTION with the corresponding REGION.REGIONDESCRIPTION where the REGION_ID applies for both and the REGIONDESCRIPTION is different:为了实现这一点,您需要在UPDATE语句中连接两个表, REGION_TMP.REGIONDESCRIPTION的值替换为相应的REGION.REGIONDESCRIPTION ,其中REGION_ID适用于两者,而REGIONDESCRIPTION不同:

UPDATE REGION_TMP
SET REGIONDESCRIPTION = REGION.REGIONDESCRIPTION
FROM REGION
WHERE REGION_TMP.REGIONID = REGION.REGIONID
  AND REGION_TMP.REGIONDESCRIPTION <> REGION.REGIONDESCRIPTION;

Check the demo here .此处查看演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何只用新值或更改值更新表? - How to update table with only new or changed values? 如何使用 Postgresql 将列更新为另一个表中的值列表? - How do I update a column to be a list of values from another table using Postgresql? 如何从 PostgreSQL 中的另一个表更新具有随机 id 的表 - How can I update a table with random ids from another table in PostgreSQL 更改另一个表中的同一列时如何更新列?(PostgreSQL) - How to update a column when the same column in another table is changed ?(postgresql) 如何用另一个表的唯一值更新一个表的列值? - How can I update column values from one table with unique values from another? 我怎样才能只更新表的某些值,这些值不是一个接一个或按顺序的,plsql 中的新值? - How can i update only some values of a table, which are not one after another or in sequence, with new ones in plsql? SQL语句:如何仅从一个表中选择一个值来更新另一个表? - SQL statement: how can I pick a value only once from a table to update another table? 如何使用行组中的值更新基于另一个表的表? - How can I UPDATE a table based on another table, using values from groups of rows? Postgresql 根据另一个表中的一组值更新列 - Postgresql update column based on set of values from another table 用另一个表的值更新表(PostgreSQL) - Update table with values of another table (PostgreSQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM