php mysql用同一个字段更新多个表?

时间:2016-08-07 17:44:38

标签: php mysql database

我在一个数据库中有5个表,并且字段名称的时间相同。我需要的东西是吼叫

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';

现在如何更新单个查询中的所有表格?我试过

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

但它给我错误

  

1052 - 列'用户名'在字段列表中是不明确的

寻找解决方案

2 个答案:

答案 0 :(得分:1)

您可以在加入的帮助下完成此操作。

  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"

答案 1 :(得分:0)

如果您想要的是在php端查询一次,我会这样做:

$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';");

如果你想在mysql端进行一次查询,那么因为那些表更新并不相互依赖,我认为没有意义,因为它会产生大量查询(正如你在其他答案中看到的那样)。