从另一个单独的表行更新一个表行

时间:2014-02-18 10:53:23

标签: mysql sql

我正在将名为allCooks的表格中的2行移动到名为recipeChallenge的表格中的一行(在披萨挑战中对cookBBB的cookAAA) 每个匹配都有一个ID,这是我要更新的行

我正在进行两次更新,一次是cookAAA,另一次是cookBBB 要用cook A更新matchID ...(我已经将php变量留在了)

"UPDATE `recipeChallenge` AS v1, 
        `allCooks` AS v2 
    SET v1.`cookAAAID` = v2.`cookID`, 
        v1.`cookAAAName` = v2.`cookName` 
  WHERE     v1.`matchID`='" .$matchID."' 
        AND v2.`cookID`= '" .$cookAID."'";

然后是厨师B

"UPDATE `recipeChallenge` AS v1, 
        `allCooks` AS v2 
    SET v1.`cookBBBID` = v2.`cookID`, 
        v1.`cookBBBName` = v2.`cookName` 
  WHERE     v1.`matchID`='" .$matchID."' 
        AND v2.`cookID`= '" .$cookBID."'";

recipeChallenge正在更新,但第二次更新还是在matchID表格中创建新的recipeChallenge和行。为什么?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

"UPDATE `recipeChallenge` AS v1, 
    `allCooks` AS v2 , 
    `allCooks` AS v3 
SET v1.`cookAAAID` = v2.`cookID`, 
    v1.`cookAAAName` = v2.`cookName`,
    v1.`cookBBBID` = v3.`cookID`, 
    v1.`cookBBBName` = v3.`cookName`
WHERE     v1.`matchID`='" .$matchID."' 
    AND v2.`cookID`= '" .$cookAID."'
    AND v3.`cookID`= '" .$cookBID."'";