22,209
社区成员
发帖
与我相关
我的任务
分享
declare @a table(id int, 内容 varchar(5))
insert into @a
select 1,'a' union all
select 2,'b' union all
select 3,'c'
declare @b table(id int, 内容 varchar(5),id2 int)
insert into @b values (1,'99',2)
--第一个问题:
select a.*,b.内容 from @b a left join @a b on a.id2=b.id
--结果
id 内容 id2 内容
----------- ----- ----------- -----
1 99 2 b
(1 行受影响)
--第二个问题
select a.*,b.内容 from @b a left join @a b on a.id2=b.id
union all
select a.*,b.内容 from @b a right join @a b on a.id2=b.id where b.内容='c'
--结果
id 内容 id2 内容
----------- ----- ----------- -----
1 99 2 b
NULL NULL NULL c
(2 行受影响)