-2

I have been trying to update some rows of a column in a database using the following statement but its not working

UPDATE member_payment AS p 
SET    debtor_receipt = (SELECT Sum(d.amount) 
                         FROM   debtor_receipt d, 
                                customer c, 
                                dairy_member m, 
                                member_payment p, 
                                vendor v 
                         WHERE  m.member_id = c.member 
                                AND d.customer = c.customer_id 
                                AND p.payee = v.vendor_id 
                                AND v.member = m.member_id 
                                AND d.txn_date = '2013-04-30') 
WHERE  p.payee IN(SELECT vendor_id 
                  FROM   vendor v, 
                         debtor_receipt d, 
                         customer c, 
                         dairy_member m, 
                         member_payment p 
                  WHERE  v.member = m.member_id 
                         AND m.member_id = c.member 
                         AND d.customer = c.customer_id 
                         AND d.txn_date = '2013-04-30') 
       AND p.txn_date = '2013-05-08'
4

2 回答 2

0

我相信您的代码失败了,因为您无法在括号之外访问 member_payment。


UPDATE member_payment AS p 
SET    debtor_receipt = (SELECT Sum(d.amount) 
                     FROM   debtor_receipt d, 
                            customer c, 
                            dairy_member m, 
                            member_payment p, 
                            vendor v 
                     WHERE  m.member_id = c.member 
                            AND d.customer = c.customer_id 
                            AND p.payee = v.vendor_id 
                            AND v.member = m.member_id 
                            AND d.txn_date = '2013-04-30'
                            AND p.payee IN(SELECT vendor_id 
                                              FROM   vendor v, 
                                                     debtor_receipt d, 
                                                     customer c, 
                                                     dairy_member m, 
                                                     member_payment p 
                                              WHERE  v.member = m.member_id 
                                                     AND m.member_id = c.member 
                                                     AND d.customer = c.customer_id 
                                                     AND d.txn_date = '2013-04-30')
                                   AND p.txn_date = '2013-05-08'                                

                            )
于 2013-08-15T20:20:41.287 回答
0

您的第二个子选择缺少 member_payment p 的连接条件,因此您获得了笛卡尔连接,这是不可取的。

这是您应该使用现代连接语法的另一个原因,因为这样您就可以准确地看到每个表是如何连接的。此外,不要在更新语句和子选择中重复使用 p 别名。例如,我通过从第一个子选择中添加连接条件 p.payee = v.vendor_id 来修补第二个子选择:

UPDATE member_payment
SET debtor_receipt = 
    (SELECT Sum(d.amount) 
    FROM debtor_receipt d
    JOIN customer c ON c.customer_id = d.customer
    JOIN dairy_member m ON m.member_id = c.member
    JOIN vendor v ON v.member = m.member_id
    JOIN member_payment p ON p.payee = v.vendor_id
    WHERE d.txn_date = '2013-04-30') 
WHERE payee IN 
    (SELECT v.vendor_id 
    FROM debtor_receipt d 
    JOIN customer c ON c.customer_id = d.customer
    JOIN dairy_member m ON m.member_id = c.member
    JOIN vendor v ON v.member = m.member_id
    JOIN member_payment p ON p.payee = v.vendor_id
    WHERE d.txn_date = '2013-04-30') 
AND txn_date = '2013-05-08'

我不确定这个更新声明试图做什么。也许您打算让第一个子选择为第二个子选择返回的每个 member_payment.payee 汇总 debtor_receipt.amount?我们还可以从子选择中删除 member_payment 表,因为 vendor 表已经包含必要的 vendor_id。就像是:

UPDATE mp
SET mp.debtor_receipt = 
    (SELECT Sum(d.amount) 
    FROM debtor_receipt d
    JOIN customer c ON c.customer_id = d.customer
    JOIN dairy_member m ON m.member_id = c.member
    JOIN vendor v ON v.member = m.member_id
    WHERE d.txn_date = '2013-04-30'
    AND v.vendor_id = mp.payee) 
FROM member_payment mp
WHERE mp.payee IN 
    (SELECT v.vendor_id 
    FROM debtor_receipt d 
    JOIN customer c ON c.customer_id = d.customer
    JOIN dairy_member m ON m.member_id = c.member
    JOIN vendor v ON v.member = m.member_id
    WHERE d.txn_date = '2013-04-30') 
AND mp.txn_date = '2013-05-08'

您应该单独运行子选择并对其进行处理,直到它们返回您期望的数据,然后将它们组合成一个更新语句来执行您想要的操作。

于 2013-05-09T16:43:03.907 回答