SQLite inner join - update using values from another table

前端 未结 5 1593
走了就别回头了
走了就别回头了 2020-12-03 05:24

This is quite easy and has been asked multiple times but I can\'t get it to work. The SQL query I think should work is:

    UPDATE table2
       SET dst.a =          


        
5条回答
  •  离开以前
    2020-12-03 06:11

    SQLITE does not support UPDATE with INNER JOIN nor do several other DB's. Inner Joins are nice and simple however it can be accomplished using just a UPDATE and a subquery select. By using a where clause and the 'IN' with a subquery and a additional subquery for the 'SET' the same result can always be accomplished. Below is how it's done.

    UPDATE table2
      SET a = a + (select a from table1 where table1.f = table2.f),
           b = b + (select b from table1 where table1.f = table2.f),
           c = c + (select c from table1 where table1.f = table2.f),
           d = d + (select d from table1 where table1.f = table2.f),
           e = e + (select e from table1 where table1.f = table2.f)
      WHERE RowId IN (Select table2.RowId from table1 where table1.f = table2.f) 
    

提交回复
热议问题