请教有关 两个 union all 连接问题
现在有两个表
表1
no version status created by
001 2 6 kitty
002 2 1 kitty
表2
no version status created by
001 0 2 ada
001 1 2 ada
002 0 2 ada
002 1 2 ada
要求的结果是
select no,version,created by from 表1 where status= '6 '
union all
select no,version,created by from 表2 where 表2.no=表1.no
即返回下列结果
no version created by
001 0 ada
001 1 ada
001 2 kitty
------解决方案--------------------select no,version,created by from 表1 where status= '6 '
union all
select no,version,created by from 表2 where exists(select * from 表1 where 表2.no=表1.no)
这样试试呢
------解决方案--------------------create table A(no varchar(10), version int, status int, [created by] varchar(10))
insert A select '001 ', 2, 6, 'kitty '
union all select '002 ', 2, 1, 'kitty '
create table B(no varchar(10), version int, status int, [created by] varchar(10))
insert B select '001 ', 0, 2, 'ada '
union all select '001 ', 1, 2, 'ada '
union all select '002 ', 0, 2, 'ada '
union all select '002 ', 1, 2, 'ada '
select no,version,[created by] from A where status= '6 '
union all
select no,version,[created by] from B where B.no in(
select distinct no from A where status= '6 '
)
order by 2
--result
no version created by
---------- ----------- ----------
001 0 ada
001 1 ada
001 2 kitty
(3 row(s) affected)
------解决方案--------------------select no,version,[