简体   繁体   English

使用另一个查询的结果更新表

[英]Update table using result of another query

I have the following query that works fine我有以下查询可以正常工作

SELECT RecordID, ROW_NUMBER() OVER (ORDER BY (Value1) DESC) AS Rank
FROM Table1

Also, I have another table(table2) that contains (among others) the fields RecordID and Rank.此外,我还有另一个表(table2),其中包含(除其他外)RecordID 和 Rank 字段。 I would like to update RecordID and Rank in table2 based on result of query above.我想根据上面的查询结果更新 table2 中的 RecordID 和 Rank。 Is that possible?那可能吗?

Yes, you can have multiple tables in an update in Postgres:是的,您可以在 Postgres 的update中包含多个表:

update table2
    set rank = t1.rank
    from (SELECT RecordID, ROW_NUMBER() OVER (ORDER BY (Value1) DESC) AS Rank
          FROM Table1
         ) t1
    where table2.RecordId = t1.RecordId;

What worked for me (in mysql ) was :对我mysql (在mysql )是:

update table2, (SELECT RecordID, ROW_NUMBER() OVER (ORDER BY (Value1) DESC) AS Rank 
    FROM Table1) tempTable 
set table2.Rank = tempTable.Rank 
where table2.RecordId = tempTable.RecordId;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM