Search code examples
sqlitesql-updateandroid-sqlite

SQLite: How can I copy data from a column in table 1 and paste the data on a column of table 2 according to IDs of the data in specific rows?


In TABLE 1 (LEFT SIDED), I have lists of words in a column(WordEng) and each of them have unique IDs listed parallel in another column(WordEngID).

enter image description here In TABLE 2 (RIGHT SIDED), I have lists of word IDs, which is nothing but unique IDs of words in TABLE 1. I want to copy words from TABLE 1 into a column in TABLE 2 according to its unique IDs. In Table 1 no words have same IDs and in Table 2 the same IDs are in different rows.

For example,

there is a word "cut" with ID "1807" in Table 1 and I want to copy and paste it in the column "words" in Table 2 which have IDs "1807"(five in this case).


Solution

  • You need this UPDATE statement:

    UPDATE Table2
    SET words = (SELECT t1.WrdEng FROM Table1 t1 WHERE t1.WrdEngId = Table2.WrdEngId)
    WHERE words IS NULL
    

    If you want all the values of words updated, even if they are not null, then remove the WHERE clause.