简体   繁体   English

如何更新postgres中其他表的值

[英]How to update a value from other tables in postgres

currently I have 3 tables目前我有 3 张桌子

Table 1:表格1:

tb1_id tb1_id values价值观
1 1 4 4

Table 2:表 2:

tb1_id tb1_id tbl3_id tbl3_id
1 1 5 5

Table 3:表3:

tb3_id tb3_id values价值观
5 5 2 2

For some reason the values from table 1 are not the same in table 3 (as shown above), I need to update the values of the table 1 with the table 3, but I am not able to do so, so far this is my query:出于某种原因,表 1 中的值与表 3 中的值不同(如上所示),我需要用表 3 更新表 1 的值,但我无法这样做,到目前为止这是我的询问:

UPDATE
    table1 t1
SET 
    values =  temp_tbl.values
FROM 
    (
        SELECT t2.tb1_id, t3.values FROM table2 t2
        JOIN 
        table3 t3 ON t2.tbl3_id = t3.tbl3_id
    ) temp_tbl
WHERE 
      t1.tbl1_id = temp_tbl.tbl1_id
AND
    t1.values != temp_tbl.values;

this is how you can do it:这是你可以做到的:

-- temporary tables -- 临时表

SELECT 1 TBL1_ID, 4 valuet1 INTO #t1 SELECT 1 TBL1_ID, 4 valuet1 INTO #t1

SELECT 1 TBL1_ID, 5 TBL3_Id INTO #t2选择 1 TBL1_ID, 5 TBL3_Id INTO #t2

SELECT 5 TBL3_ID, 2 valuet3 INTO #t3 SELECT 5 TBL3_ID, 2 valuet3 INTO #t3

Blockquote块引用

-- select with joins -- 选择连接

select T1.选择 T1。 ,T2. ,T2。 , T3.* from #t1 T1 INNER JOIN #t2 T2 ON T1.TBL1_ID = T2.TBL1_ID INNER JOIN #t3 T3 ON T3.TBL3_ID = T2.TBL3_ID , T3.* from #t1 T1 INNER JOIN #t2 T2 ON T1.TBL1_ID = T2.TBL1_ID INNER JOIN #t3 T3 ON T3.TBL3_ID = T2.TBL3_ID

-- update would be like this -- 更新会是这样

update #t1 set valuet1 = T3.valuet3 from #t1 T1 INNER JOIN #t2 T2 ON T1.TBL1_ID = T2.TBL1_ID INNER JOIN #t3 T3 ON T3.TBL3_ID = T2.TBL3_ID update #t1 set valuet1 = T3.valuet3 from #t1 T1 INNER JOIN #t2 T2 ON T1.TBL1_ID = T2.TBL1_ID INNER JOIN #t3 T3 ON T3.TBL3_ID = T2.TBL3_ID

OK so if I understood you correctly, you just need to make a slight correction to your code:好的,如果我理解正确,您只需要对您的代码稍作修正:

UPDATE
    table1 t1
SET 
    values =  temp_tbl.values
FROM 
    (
        SELECT t2.tbl1_id, t3.values 
        FROM table2 t2
        Inner JOIN table3 t3 ON t2.tbl3_id = t3.tbl3_id
    ) temp_tbl
WHERE 
      t1.tbl1_id = temp_tbl.tbl1_id
AND
    t1.values != temp_tbl.values;

Here is DBFiddle link.这是DBFiddle 链接。

EDIT: This would also do:编辑:这也可以:

with newData as (
   select t1.tbl1_id, t3.Values
   from Table1 t1
   inner join Table2 t2 on t1.Tbl1_Id = t2.Tbl1_Id
   inner join Table3 t3 on t2.Tbl3_Id = t3.Tbl3_Id
   where t1.Values != t3.Values
)
UPDATE
    table1
SET 
    values = newData.Values
FROM newData 
where table1.tbl1_id = newData.tbl1_id;

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

相关问题 如何在Postgres中将表从公共表移动到其他模式 - how to move tables from public to other schema in Postgres Oracle SQL:更新与其他两个表的值不同的表 - Oracle SQL: Update a table with difference in value from two other tables 如何使用 MySQL 中其他两个表的值总和更新表 - How to update a table with sum of values from two other tables in MySQL 如何用其他表中的不同值更新多行? - How to update multiple lines with different values from other tables? 如何让python从其他数据不完整的表更新MYSQL? - How to make python update MYSQL from other tables with incomplete data? 使用与其他两个表匹配的值更新列 - Update a colum with a Value that matches in two other tables 更新表的多行,该表的特定列中的特定值等于从其他两个表中计算出的值 - Update multiple rows of a table having specific value in particular column equal to value calculated from two other tables Postgres从其他一些与主表相关的表中进行选择 - Postgres select from few other tables with relation to main table 有关从Postgres db上的其他表获取计数的查询建议 - suggestions on query to get counts from other tables on a postgres db 如何获取具有最大值的行并且它与另外两个表匹配? - how to get row with max value and it matches from two other tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM