Php 对多个行/列使用一个更新查询
我将如何将代码放入一个UPDATE语句中,记住我正在执行许多条件语句:Php 对多个行/列使用一个更新查询,php,mysql,sql-update,Php,Mysql,Sql Update,我将如何将代码放入一个UPDATE语句中,记住我正在执行许多条件语句: $updateTbl1 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerAge = t.customerAge WHERE o.id = t.id AND (o.customerAge != t.customerAge );"; $updateTbl2 = "UPDATE $tableNameOrig o, $tableNameTe
$updateTbl1 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerAge = t.customerAge WHERE o.id = t.id AND (o.customerAge != t.customerAge );";
$updateTbl2 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerCode = t.customerCode WHERE o.id = t.id AND (o.customerCode != t.customerCode);";
$updateTbl3 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerName = t.customerName WHERE o.id = t.id AND (o.customerName != t.customerName);";
$updateTbl4 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerEmail = t.customerEmail WHERE o.id = t.id AND (o.customerEmail != t.customerEmail);";
$updateTbl5 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerAddress = t.customerAddress WHERE o.id = t.id AND (o.customerAddress != t.customerAddress);";
$updateTbl6 = "UPDATE $tableNameOrig o, $tableNameTemp t SET o.customerAdded = t.customerAdded WHERE o.id = t.id AND (o.customerAdded != t.customerAdded);";
(mysqli_query($con,$updateTbl1));
(mysqli_query($con,$updateTbl2));
(mysqli_query($con,$updateTbl3));
(mysqli_query($con,$updateTbl4));
(mysqli_query($con,$updateTbl5));
(mysqli_query($con,$updateTbl6));
谢谢
UPDATE $tableNameOrig o
JOIN $tableNameTemp t
ON o.id = t.id
SET o.customerAge = t.customerAge,
o.customerCode = t.customerCode,
o.customerName = t.customerName,
o.customerEmail = t.customerEmail,
o.customerAddress = t.customerAddress,
o.customerAdded = t.customerAdded
您的其他条件是没有意义的。实际上并没有使用一个
更新
,而是使用一个PHP方法调用,mysqli\u multi\u查询($con,$updateTbl1.$updateTbl2)代码>等等。但我只想在值不同时更新它?这就是为什么我要做一个条件测试,因为值不相等。@LazyTotoro是的,但如果值相同,那就没什么大不了的了。。我敢肯定这是最快的方法。MySQL让你知道有多少行是匹配的,有多少行是更改的。我现在意识到了这一点,表中还有一列不会得到批量更新,所以我可以在查询中省去它。谢谢你的帮助和解释!