对一个以上的行/列使用一个UPDATE查询

时间:2014-07-29 13:15:30

标签: 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, $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));

谢谢!

1 个答案:

答案 0 :(得分:2)

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

你的其他条件毫无意义。