Home > OS >  Update data from second table by third table
Update data from second table by third table

Time:04-24

I have 3 tables and I want to update the first table from second table by third table.

The ID from table1 and table2 are the same and uid from table2 and table3 are the same. I want to copy aaa to second row in table1.

table1

| ID       | value          |
| -------- | -------------- |
| 1        | apple          |
| 2        | banana         |

table2

| ID       | uid          |
| -------- | ------------ |
| 1        | 11           |
| 2        | 10           |

table3

| ID       | uid            | value          |
| -------- | -------------- | -------------- |
| 3        | 10             | aaa            |
| 4        | 11             | bbb            |

CodePudding user response:

You can join all 3 tables in the UPDATE statement:

UPDATE table1 t1
INNER JOIN table2 t2 ON t2.ID = t1.ID
INNER JOIN table3 t3 ON t3.uid = t2.uid
SET t1.value = t3.value;

CodePudding user response:

You can joined all the table and specify where clause for your requirment

UPDATE table1 t1 JOIN table2 t2 ON t1.id = t2.id JOIN table3 t3 ON t3.uid = t2.uid SET t1.value = t3.value Where t1.id = 2;
  • Related