JavaCake
JavaCake

Reputation: 4115

Update table with data from secondary table by foreignkey

I have two tables: tableA and tableB. I want to remove tableB hence it is redundant, so i need to copy the column values from tableB to tableA.

tableA:

|    id    |    name    |    tableA_id    |

tableB

|    id    |    tableC_id    |    tableD_id    |

tableA has been altered so it has the additional columns as tableA:

|    id    |    name    |    tableA_id    |    tableC_id    |    tableD_id    |

So basically i want to copy the columns tableB.tableC_id and tableB.tableD_id into tableA.tableC_id and tableA.tableD_id.

How can i pull this off in a SQL update?

Upvotes: 0

Views: 56

Answers (1)

flaschenpost
flaschenpost

Reputation: 2235

You update with an UPDATE. ;-)

UPDATE tableA inner join tableB on tableA.id = tableB.id set tableA.tableC_id = tableB.tableC_id, tableA.tableD_id = tableB.tableD_id;

Upvotes: 1

Related Questions