我已经尝试过这段代码但它只能在每个客户的第一个实例上运行。即列应该贯穿并将所有订单添加到之前的欠款中。
UPDATE customers_tbl
SET customer_amountowing = customer_amountowing +
(SELECT sorder_amount FROM standingorder_tbl
WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1)
WHERE EXISTS
( SELECT * FROM standingorder_tbl
WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1 );
谁能告诉我,我在这里出错了。干杯们!!
答案 0 :(得分:1)
如果要添加所有先前订单的总和,则应以下列方式在子查询中使用SUM函数:
UPDATE customers_tbl
SET customer_amountowing = customer_amountowing +
(SELECT SUM(sorder_amount) FROM standingorder_tbl
WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1)
WHERE EXISTS
( SELECT * FROM standingorder_tbl
WHERE standingorder_tbl.sorder_customer = customers_tbl.customer_address1 );