联接 - 从辅助表中选择列

问题描述 投票:0回答:1

sc_new
位于表
B.Id
中时,我尝试将
billing_helper
更新为“Z5”,否则必须将其设置为“Z1”。任何建议将不胜感激。

select 
    sc_trf, 
    sc_new = case 
                 when B.ID is not null 
                     then 'Z5'
                     else 'Z1'
             end
from 
    billing a
left join 
    billing_helper b on a.cust_id = b.id
sql sql-server join
1个回答
0
投票

这是一些假设数据

declare @billing TABLE (cust_id int, sc_trf varchar(10))
declare @billing_helper TABLE (id int, sc_new varchar(10))

insert into @billing_helper VALUES (1, null), (2, null)
insert into @billing VALUES
(1, 'aaa'), (2, 'bbb'), (5, 'ccc')

select 
    sc_trf, 
    b.sc_new,
    sc_new = case when B.ID is not null then 'Z5' else 'Z1' end
from @billing a
left join @billing_helper b on a.cust_id = b.id

select * from @billing_helper

update BH SET
sc_new = case when BH.ID is not null then 'Z5' else 'Z1' end
from @billing a
left join @billing_helper bh on a.cust_id = bh.id

select * from @billing_helper

结果将是选择,更新前和更新后

© www.soinside.com 2019 - 2024. All rights reserved.