简体   繁体   中英

Using one UPDATE query for more than a single row/ colum

How would I go about putting my code into one UPDATE statement, bearing in mind that i'm doing a lot of conditional statements:

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

Thanks!

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

Your other conditions are meaningless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM