基于连接的SQL更新值

时间:2017-09-17 06:58:00

标签: sql oracle

我正在尝试根据第二个表的条件更新一个表的值。 但有些事情是错的。你能告诉我我做错了什么吗?

UPDATE food_serve
SET food_serve_cost = food_serve_cost*1.15
FROM food_serve JOIN fooditem  
ON fooditem.food_item_no = food_serve.food_item_no
WHERE food_type = 'M' ;

使用oracle sql。错误报告 -

  

SQL错误:ORA-00933:SQL命令未正确结束   00933. 00000 - “SQL命令未正确结束”   *原因:
  *操作:

3 个答案:

答案 0 :(得分:0)

似乎你有不明确的列尝试添加表名,例如:

UPDATE food_serve
SET food_serve.food_serve_cost = fooditem.food_serve_cost*1.15
FROM food_serve JOIN fooditem  
ON fooditem.food_item_no = food_serve.food_item_no
WHERE fooditem.food_type = 'M' ;

答案 1 :(得分:0)

您好我认为您遗漏了food_serve_cost中的某些内容您没有使用任何别名

UPDATE ( SELECT food_serve.food_serve_cost , 
fooditem.food_serve_cost
FROM food_serve
INNER JOIN fooditem ON fooditem.food_item_no =   
food_serve.food_item_no
WHERE fooditem.food_type = 'M')
SET food_serve.food_serve_cost=fooditem.food_serve_cost*1.15

或者您可以使用合并

MERGE into food_serve
USING fooditem
ON (fooditem.food_item_no = food_serve.food_item_no)
when matched then update SET     
food_serve.food_serve_cost=fooditem.food_serve_cost*1.15
WHERE fooditem.food_type = 'M'

答案 2 :(得分:0)

在这种情况下,您可以更好地利用合并运营商

               select * into #Target from fooditem  WHERE food_type ='M' 

                     MERGE food_serve AS T
                     USING #Target AS S 
                     ON S.food_item_no = T.food_item_no 
                     WHEN MATCHED THEN
                     UPDATE SET T.food_serve_cost = T.food_serve_cost*1.15