user1877978
user1877978

Reputation:

php mysql update multiple table with same field?

I have 5 table in a single database and same time of field name. the things i need to is bellow

UPDATE `customers` SET  `username`='new' WHERE `username`='old';
UPDATE `radacct` SET `username`='new' WHERE `username`='old';
UPDATE `radcheck` SET `username`='new' WHERE `username`='old';
UPDATE `radreply` SET `username`='new' WHERE `username`='old';
UPDATE `radusergroup` SET  `username`='new' WHERE `username`='old';

now how can i update all table in a single query? I have tried with

UPDATE `customers`,`radacct`,`radcheck`,`radreply`,`radusergroup` SET  `username`='new' WHERE `username`='old' 

but its giving me error

1052 - Column 'username' in field list is ambiguous

looking for solution

Upvotes: 0

Views: 1341

Answers (2)

Kameneth
Kameneth

Reputation: 112

If what you want is one call to query on php side, i would do:

$mysqli->query("UPDATE `customers` SET  `username`='new' WHERE `username`='old'; UPDATE `radacct` SET `username`='new' WHERE `username`='old'; UPDATE `radcheck` SET `username`='new' WHERE `username`='old'; UPDATE `radreply` SET `username`='new' WHERE `username`='old'; UPDATE `radusergroup` SET  `username`='new' WHERE `username`='old';");

If you want one query on mysql side, well since those table updates are not dependant from each other, i think there is no point as it makes a heavy query (as you can see on other answer).

Upvotes: 0

vipin sharma
vipin sharma

Reputation: 156

You can do this with help of join.

  UPDATE customers, 
           radacct, 
           radcheck,
           radreply,
           radusergroup  
    SET    customers.username = "new", 
           radacct.username = "new", 
           radcheck.username = "new"
           radreply.username = "new"    
           radusergroup.username = "new"
   WHERE   customers.username = "old"
            AND radacct.username = "old"
            AND radcheck.username = "old" 
            AND radreply.username = "old"  
            AND radusergroup.username = "old"

Upvotes: 1

Related Questions