Update a table with values from a related table in SQLite

Updating a column in a table based on values in a second table proved to be quite a difficult operation in SQLite. It’s not as straightforward as it is in MySQL where one can use an inner join.

Here’s the problem:
You have two tables and you want to copy the values from a column in table 2 and add them to an equivalent column in table 1. Both tables have the same ID field that you are using to join the tables. This is how you can do it:

UPDATE 'table1' SET target_column=(
SELECT table2.target_column
FROM table2
WHERE 'table1'.id_column = table2.id_column);

Note: I’m assuming the column names are exaclty the same in both columns.


Posted

in

by

Tags: