Update column with value from another table using SQLite?
I am assuming that the t2.trans_id
is uniq
or primary key
in table2
. If not then if it return multiple result then the update query will blow up. In that case either you need to apply the more filter using the WHERE
or use the TOP 1
if any result will be needed.
UPDATE table1
SET status = (SELECT t2.status FROM table2 t2 WHERE t2.trans_id = id) ,
name = (SELECT t2.name FROM table2 t2 WHERE t2.trans_id = id)
WHERE id IN (SELECT trans_id FROM table2 t2 WHERE t2.trans_id= id)
The previous answer will be failed if there is the id column in the table2. It would be better that using the full name table1.id
UPDATE table1
SET status = (SELECT t2.status FROM table2 t2 WHERE t2.trans_id = table1.id) ,
name = (SELECT t2.name FROM table2 t2 WHERE t2.trans_id = table1.id)
WHERE id IN (SELECT trans_id FROM table2 t2 WHERE t2.trans_id= table1.id);