2017-04-07 94 views
0

我試圖從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

我想你想要一個相關的子查詢。您也有rownum的問題 - 您在之前選擇任意行order byorder 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 
        ); 
+0

它的工作原理。感謝磨坊! :) – Michael