我正在尝试将select语句中的最后一个值输入到更新中。我已经尝试了下面的查询,但得到了错误:这给了我错误:缺少右括号。
update dt2112b2.postgraduate_order set order_total =
(select item_cost * order_quantity
FROM dt2112b2.order_stock, dt2112b2.postgraduate_order
WHERE dt2112b2.order_stock.menu_item =
dt2112b2.postgraduate_order.menu_item and ROWNUM <=1
ORDER BY postgraduate_order.order_id desc);
答案 0 :(得分:1)
我认为你想要一个相关的子查询。您还遇到了rownum
的问题 - 您在 order by
之前选择了一个任意行。 order by
什么也没做。也许你真的想要总和:
update dt2112b2.postgraduate_order po
set order_total = (select sum(item_cost * order_quantity)
from dt2112b2.order_stock os
where os.menu_item = po.menu_item
);
或者,如果您确实需要具有最大订单ID的匹配订单:
update dt2112b2.postgraduate_order po
set order_total = (select max(item_cost * order_quantity) keep (dense_rank first order by order_id desc)
from dt2112b2.order_stock os
where os.menu_item = po.menu_item
);