我想根据另一个表(table2)中一个或多个字段的值更新表(table1)。我认为这应该是一个案例陈述但我不确定如何在一个语句中基于另一个表合并case语句和update子句。这是我到目前为止所知道的不起作用:
update table1 i, table2 s
set i.sales = 'F'
where s.payment = 'Y'
and i.order_no = s.order_no;
我知道如何基于两个表进行选择,但这不是很有用,因为我不想创建新的数据库对象 - 我只想更新现有对象(table1):
create or replace view merge as
select
i.order_no
, case when s.payment = 'Y'
then 'F'
end as sales
from table1 i, table2 s
where i.order_no = s.order_no;
我知道如何在案例陈述中更新WITHIN:
UPDATE table1
SET sales = (
SELECT CASE
WHEN foo = 'X'
THEN 'F'
ELSE null
END
FROM table1
)
;
我考虑了where子句而不是case语句,但它最终选择了每条记录,而第二个表在付款字段中肯定有不同的值:
update t1
set sales = 'F'
where exists (select table2.payment
from table2
where table2.order_no = table1.order_no
and table2.payment = 'Y');
答案 0 :(得分:0)
试试这个:
update table1 i
set i.sales = (select case when x.payment = 'Y'
then 'F'
else i.sales end
from table2 x
where x.order_no = i.order_no);
答案 1 :(得分:0)
我现在没有运行oracle(所以我无法检查语法),但我认为你可以尝试这样的事情:
update table1 i
set i.sales = 'F'
where i.order_no IN (select s.order_no from table2 s where s.payment = 'Y')
希望它有所帮助!