Update table using result of another query
What worked for me (in mysql
) was :
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;
Yes, you can have multiple tables in an update
in Postgres:
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;