I have two tables in SQLite which look like this
TABLE_X
____________________
| id | C1 | C2 | C3 | C4 |
| 10 | 99 | 03 | 04 | 00 |
| 60 | 88 | 20 | 30 | 40 |
TABLE_Y
___________
| id | C2 |
| 10 | 11 |
| 60 | 22 |
I am trying to write query to update records on Table X based on records in Table Y The condition for the updateis something like the following
update table_x
set table_x.c1 = 100,
table_x.c2 = table_y.c2
where table_x.id = table_y.id
But when I try to do this I get an error message saying
NO such column: table_y.c2
The deleted answer was correct about the cause of the error: a relation identifier must be introduced (e.g. with FROM/JOIN) in a query before it can be used.
While SQLite does not support UPDATE..JOIN
(so there is no way to introduce the lookup relation directly), a dependent sub-query can be used to simulate the effect:
update table_x
set c1 = 100,
c2 = (select y.c2 from table_y as y
where y.id = table_x.id)
Note that unlike a proper UPDATE..JOIN
, if the sub-select fails to find a match then NULL will be assigned.
YMMV.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments