sql语句 根据条件update

2025-03-24 07:03:46
推荐回答(3个)
回答(1):

update B
set B.div = (case
when B.id in (select distinct id from A) then (select div from A where id = B.id)
else (select div from A where id='00') end
)
from A,B
where B.div is null

回答(2):

先运行
update B set div = a. a2
from
(
select ID as a1
,DIV as a2
from A
) as a
where div is null
and ID = a.a1

再运行
update B set div = a. a2
from
(
select ID as a1
,DIV as a2
from A
where ID = 00
) as a
where div is null

回答(3):

update b
set b.div = (case
when b.id in (select distinct div from a) then a.div
when b.id not in (select distinct div from a) then '00'
)
from a,b
where b.div is null