A
_______________
id value
1 1
2 2
3 3
4 10
5 11B
_______________
id value rAid
1 1 1
2 2 1
3 3 1
4 7 2
5 8 3
6 8 9
7 8 10把B表不存在A的值更新‘AAA’
使用sqlserver语句
查询B表结果:
id value
1 1
2 1
3 1
4 2
5 3
6 AAA
7 AAA
_______________
id value
1 1
2 2
3 3
4 10
5 11B
_______________
id value rAid
1 1 1
2 2 1
3 3 1
4 7 2
5 8 3
6 8 9
7 8 10把B表不存在A的值更新‘AAA’
使用sqlserver语句
查询B表结果:
id value
1 1
2 1
3 1
4 2
5 3
6 AAA
7 AAA
from B
where not exists(select * from A where A.id=B.id)
select b.id,(case when a.id is not null then ltrim(value) else 'AAA') val
from b left join a on b.rAid = a.value
use tempdb;
/*
create table A
(
id int not null,
value int not null
);
insert into A(id,value)
values
(1,1),
(2,2),
(3,3),
(4,10),
(5,11);create table B
(
id int not null,
value int not null,
rAid nvarchar(10) not null
);
insert into B(id,value,rAid)
values
(1,1,'1'),
(2,2,'1'),
(3,3,'1'),
(4,7,'2'),
(5,8,'3'),
(6,8,'9'),
(7,8,'10');
*/
select B.id,
case when A.value IS null then 'AAA' else B.rAid end as [value]
from B
left join A on A.id = B.id;