选择要更新的值

时间:2017-04-07 10:59:49

标签: sql oracle

我正在尝试将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);

1 个答案:

答案 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 
                      );