0

我必须将客户列的总订单更新为等于客户下的所有订单的总数(在 cust_order 中)在此处输入图像描述

这是我尝试过的

update (select * 
        from atish_customer a 
        inner join 
        (
            select cust_nbr,count(cust_nbr) as count_orders
            from atish_cust_order 
            group by cust_nbr
        )c
        on c.cust_nbr=a.cust_nbr) 
set tot_orders=count_orders;

但这是我得到的错误

ORA-01779: cannot modify a column which maps to a non key-preserved table
4

1 回答 1

2

这个怎么样:

UPDATE customer SET total_orders = (
    SELECT COUNT(*) FROM cust_order 
    WHERE cust_order.cust_nbr = customer.cust_nbr
) 

[我不确定你的atish_customer和在哪里atish_customer_order发挥作用......它们没有显示在你的图表中]

解释:基本上内部选择只是计算来自 cust_order 表的每个 cust_nbr 的订单数。通过将外部连接customer.cust_nbr到内部cust_order.cust_nbr,每个 [外部] 行都将更新为正确的总数。这称为相关子查询(参见此处的简短教程)。

于 2012-07-25T14:47:10.203 回答