0

Hi guys, I want to update columns from two tables.

For example: abc and xyz. The abc table contains fields called id, fname, lastname, status. And the table xyz contains id, x_id and status. Now what I want is to update a column status from abc and status column from xyz by using single query.

I try this query, but it is not working.

UPDATE abc a1 
JOIN xyz x1 ON a1.id = x1.x_id 
SET a1.status = "D" , 
    x1.status = "delete" 
WHERE a1.id = 15 AND x1.x_id = 15;  

Thanks.

Tjoene
  • 320
  • 9
  • 18
Akki
  • 9
  • 3
  • 5
    Check: http://stackoverflow.com/questions/4361774/mysql-update-multiple-tables-with-one-query and http://stackoverflow.com/questions/1871537/how-to-update-two-tables-in-one-statement – Anda Iancu Feb 15 '13 at 07:56
  • +1 for linking to relevant links – Khez Feb 15 '13 at 07:58

2 Answers2

0

Edited for 3 tables:

UPDATE a1,x1,s1 
SET a1.status = "D" , 
x1.status = "delte",
s1.status = "D" 
WHERE a1.id = x1.x_id
AND a1.id = s1.s_id 
AND a1.id = 15;
Bojan Kovacevic
  • 778
  • 7
  • 19
0

If your mysql server cannot update two tables in a single query using SQL. You can use LOCK TABLES command to avoid race condition :

LOCK TABLES abc WRITE, xyz WRITE;
update abc set status = "D"  where id = 15;
update xyz set status = "delete"  where x_id = 15;
UNLOCK TABLES;

Regards, Omar

Omar MEBARKI
  • 647
  • 4
  • 8