当前位置: 代码迷 >> Sql Server >> union all链接三个表解决思路
  详细解决方案

union all链接三个表解决思路

热度:87   发布时间:2016-04-27 21:31:47.0
union all链接三个表
select       a,b..       from           table1      
union   all
select       a1,b..from           table2
union   all
select       a3,b..from           table3

我查询这三个表,现在我想把我所有的查询结果只要2000条,该如何取舍??????

------解决方案--------------------
try

Select TOP 2000 * From
(
select a,b.. from table1
union all
select a1,b..from table2
union all
select a3,b..from table3) A
------解决方案--------------------
create table table1(
a varchar(10),
b varchar(10)
)
create table table2(
a1 varchar(10),
b varchar(10)
)
create table table3(
a3 varchar(10),
b varchar(10)
)

insert into table1
select 'test1 ', 'test1 ' union all
select 'test2 ', 'test2 '
insert into table2
select 'test3 ', 'test3 ' union all
select 'test4 ', 'test4 '
insert into table3
select 'test5 ', 'test5 '

Select TOP 2000 * From
(
select a,b from table1
union all
select a1,b from table2
union all
select a3,b from table3) A


没问题啊?
------解决方案--------------------

set rowcount 2000
select a,b.. from table1
union all
select a1,b..from table2
union all
select a3,b..from table3

------解决方案--------------------
set rowcount 2000
------解决方案--------------------
Select Top 2000 * From (
Select Top 2000 a,b,... From table1
Union
Select Top 2000 a,b,... From table2
Union
Select Top 2000 a,b,... From table3
) a
這樣寫在一些情況下會好些