user3350593
user3350593

Reputation:

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!

Upvotes: 0

Views: 39

Answers (1)

Arth
Arth

Reputation: 13110

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.

Upvotes: 1

Related Questions