使用另一个表中的数据更新列

时间:2013-05-09 15:13:03

标签: sql

我一直在尝试使用以下语句更新数据库中列的某些行,但它不能正常工作

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'

2 个答案:

答案 0 :(得分:0)

你的第二个subselect缺少member_payment p的连接条件,所以你得到了一个笛卡尔连接,这是不可取的。

这是您应该使用现代连接语法的另一个原因,因为这样您就可以确切了解每个表的连接方式。另外,请勿在update语句和子选择中重复使用p alias。例如,我通过在第一个子选择中添加连接条件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'

我不确定此更新声明正在尝试做什么。也许你打算让第一个subselect为第二个subselect返回的每个member_payment.payee总结debtor_receipt.amount?我们还可以从子选择中删除member_payment表,因为供应商表已经包含必要的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'

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

答案 1 :(得分: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'                                

                            )