使用LEFT JOIN进行UPDATE的语法

时间:2014-04-11 18:40:20

标签: mysql join sql-update

如何使用更新插入此查询?我已经尝试了所有可能的组合,似乎没有任何效果!

UPDATE Test_table2
SET Pledge_Status = closed
WHERE (
    SELECT SUM(pledgers.Pledge_payment_amt) AS pledged
    FROM Test_table2  AS recipients
    LEFT JOIN Test_table2 AS pledgers
    ON recipients.GIFT_NO = pledgers.PLEDGE_GIFT_NO
    GROUP BY recipients.GIFT_NO
    HAVING recipients.Pledge_Amt >= pledged
    ORDER BY recipients.CRSID ASC
);

架构(所有varchar):

  

ID,Name,Pledge_Amount,Pledge_payment_amt,Gift_No,Pledge_Gift_No,Pledge_Open / Closed

非常感谢你!

1 个答案:

答案 0 :(得分:0)

this question平行我建议您使用WHERE ... IN SELECT。此外,您需要将每行的ID从子查询传递到主查询,并且由于您没有提供表模式,我们只能在此猜测:

UPDATE Test_table2
SET Pledge_Status = closed
WHERE [your id column] IN (
    SELECT [your id column]
    FROM Test_table2  AS recipients
    LEFT JOIN Test_table2 AS pledgers
    ON recipients.GIFT_NO = pledgers.PLEDGE_GIFT_NO
    GROUP BY recipients.GIFT_NO
    HAVING recipients.Pledge_Amt >= SUM(pledgers.Pledge_payment_amt)
    /*ORDER BY recipients.CRSID ASC <-- no need to order anything here */
);