Update column with value from another table using SQLite?

Chirag B

I have two SQLite tables. I want to update a column in table1 with a value from table2.

Table 1, table1 (id INTEGER AUTOINCREMENT, status TEXT, name TEXT);:

| id |  status   | name |
|----|-----------|------|
|  1 | pending   | xyz  |
|  2 | completed | abc  |

Table 2, table2 (status TEXT, name TEXT, trans_id INTEGER);:

| trans_id |  status   | name |
|----------|-----------|------|
|        1 | refunded  | cvb  |
|        2 | cancelled | asd  |

I want to update status and name from table2 to table1 where table1.id = table2.trans_id. I have this query:

UPDATE table1 
SET status = (SELECT t2.status FROM table1 t1,table2 t2 WHERE t1.id = t2.trans_id) , 
name = (SELECT t2.name FROM table1 t1,table2 t2 WHERE t1.id = t2.trans_id)
WHERE id IN (SELECT trans_id FROM table1 t1,table2 t2 WHERE t1.id = t2.trans_id)

It populates table1 wrongly. This is the resultant table1

| id |  status  | name |
|----|----------|------|
|  1 | refunded | cvb  |
|  2 | refunded | cvb  |

My requirement is this:

| id |  status   | name |
|----|-----------|------|
|  1 | refunded  | cvb  |
|  2 | cancelled | asd  |

Whats wrong with my query? How can I achieve it?

Mahesh

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update table column with another column values of the same table using updateAll()

From Dev

Update a table column from the column of another table in MsAccess

From Dev

Update a column of NAs in one data table with the value from a column in another data table

From Dev

Update Table With An Empty Column With Data From Another Table using a shared id from a third table

From Dev

Update a Column from another Column using SQLite?

From Dev

Update column value by querying concatenated column values with another table value

From Dev

How to update mysql column with another value from another table?

From Dev

SQLite - Update a column based on values from another table's columns

From Dev

MySQL - Update/Set a column in one table equal to MAX value from another table

From Dev

Update a column from a SELECT of another table

From Dev

SQLite select average from one table and update average field from another table using trigger

From Dev

How to Update Column from a Column in another table

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

mysql update an column from another table

From Dev

update table based on another tables value in sqLite

From Dev

Update Table With An Empty Column With Data From Another Table using a shared id from a third table

From Dev

How to update mysql column with another value from another table?

From Dev

Access: UPDATE column with values from another table

From Dev

Update newly added column with value on another table

From Dev

How to display value of a field from a different table by using the value of a different column in another table

From Dev

oracle update first 4 characters with column value from another table

From Dev

Update column from another table column value

From Dev

How to update table column's value from another table in sequence when table do not relate with each other

From Dev

SQLite: updating column from another table

From Dev

Update table's column with values from another table's column

From Dev

mysql update column value from another table

From Dev

MySQL update column from another table

From Dev

How to update a column using another column value?

From Dev

update column values by looking up value from another table

Related Related

  1. 1

    Update table column with another column values of the same table using updateAll()

  2. 2

    Update a table column from the column of another table in MsAccess

  3. 3

    Update a column of NAs in one data table with the value from a column in another data table

  4. 4

    Update Table With An Empty Column With Data From Another Table using a shared id from a third table

  5. 5

    Update a Column from another Column using SQLite?

  6. 6

    Update column value by querying concatenated column values with another table value

  7. 7

    How to update mysql column with another value from another table?

  8. 8

    SQLite - Update a column based on values from another table's columns

  9. 9

    MySQL - Update/Set a column in one table equal to MAX value from another table

  10. 10

    Update a column from a SELECT of another table

  11. 11

    SQLite select average from one table and update average field from another table using trigger

  12. 12

    How to Update Column from a Column in another table

  13. 13

    Column loop and update another column with COUNT() value from another table

  14. 14

    mysql update an column from another table

  15. 15

    update table based on another tables value in sqLite

  16. 16

    Update Table With An Empty Column With Data From Another Table using a shared id from a third table

  17. 17

    How to update mysql column with another value from another table?

  18. 18

    Access: UPDATE column with values from another table

  19. 19

    Update newly added column with value on another table

  20. 20

    How to display value of a field from a different table by using the value of a different column in another table

  21. 21

    oracle update first 4 characters with column value from another table

  22. 22

    Update column from another table column value

  23. 23

    How to update table column's value from another table in sequence when table do not relate with each other

  24. 24

    SQLite: updating column from another table

  25. 25

    Update table's column with values from another table's column

  26. 26

    mysql update column value from another table

  27. 27

    MySQL update column from another table

  28. 28

    How to update a column using another column value?

  29. 29

    update column values by looking up value from another table

HotTag

Archive