Updating multiple tables with one query using MYSQL

edited May 2023 in PHP

In multiple tables update, it updates rows in each specified table that satisfy the conditions. Each matching row is updated once, even if it matches the conditions multiple times.

The syntax for multi-table UPDATE is:

UPDATE

table1, table2, ...

  SET column1 = expression1,

    column2 = expression2,

  [WHERE conditions]

For example, consider two tables,  crm_lead and  crm_account. In case we are updating the acc_manager_id and owner_id.

query :

$sql = "UPDATE crm_account ca,crm_lead cl SET cl.acc_manager_id= ca.acc_manager_id,cl.owner_id= ca.owner_id WHERE ca.id=cl.acc_id";

Thanks for reading this article, I hope it is useful to you.

Tagged:
Sign In or Register to comment.