MySQL update table based on value of another table Join

While working on MySQL, sometimes we need to update the table values based on the values of another table. In this case, we can update the table value by joining from another table. MySQL table join

Here, I have done this as follows:

For example, we need to update the value to table2 from the value of table1

UPDATE table2
       JOIN table1
       ON table2.callid = table1.callid
SET    table2.duration = table1.billsec;

We can put a where clause also like this if required.

UPDATE table2
       JOIN table1
       ON table2.callid = table1.callid
SET    table2.duration = table1.billsec 
where table2.duration='';

Learn MoreMySQL CREATE TABLE Statement , Joining to different databases table

Leave a Reply