我必须将客户列的total_orders更新为等于客户放置的所有订单的总数(在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
答案 0 :(得分: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_nbr的cust_order表中的订单数。通过将外部customer.cust_nbr
连接到内部cust_order.cust_nbr
,每个[外部]行将使用正确的总数进行更新。这称为相关子查询(有关简短教程,请参阅here)。