首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

union all链接三个表解决思路

2012-01-21 
union all链接三个表selecta,b..fromtable1unionallselecta1,b..fromtable2unionallselecta3,b..fromtable

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
這樣寫在一些情況下會好些

热点排行