sql语句 根据条件update
答案:3 悬赏:20 手机版
解决时间 2021-01-18 04:29
- 提问者网友:咪咪
- 2021-01-17 13:51
sql语句 根据条件update
最佳答案
- 五星知识达人网友:鸠书
- 2021-01-17 14:35
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
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
全部回答
- 1楼网友:上分大魔王
- 2021-01-17 16:28
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追问感谢回答,你可能理解错了我的意思。
当B表的ID在A表里不存在时,是用A表里面ID为00的DIV更新,而不是用“00”来更新。
不过还是很感谢。
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追问感谢回答,你可能理解错了我的意思。
当B表的ID在A表里不存在时,是用A表里面ID为00的DIV更新,而不是用“00”来更新。
不过还是很感谢。
- 2楼网友:归鹤鸣
- 2021-01-17 16:08
先运行
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追问感谢回答,第一段SQL语句是把ID相同的先更新掉,第二段是把ID不同的固定用00的DIV来更新。
能不能用一段SQL语句来做呢?追答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追问感谢回答,第一段SQL语句是把ID相同的先更新掉,第二段是把ID不同的固定用00的DIV来更新。
能不能用一段SQL语句来做呢?追答2个语句可以放在一起运行的,先后顺序不要搞反
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯