【发布时间】:2014-01-14 21:03:15
【问题描述】:
我想根据另一个表 (table2) 中一个或多个字段的值更新表 (table1)。我相信这应该是一个 case 语句,但我不确定如何在一个语句中合并一个 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;
而且我知道如何在 case 语句中更新:
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');
【问题讨论】: