读书人

怎么合并同一表中通过不同查询条件产生

发布时间: 2012-01-28 22:06:14 作者: rapoo

如何合并同一表中通过不同查询条件产生的结果集
如:
select top 10 * from Article where ClassID=1
select top 10 * from Article where ClassID=2
select top 10 * from Article where ClassID=3

要把这30条记录合并成一个结果集,该怎么做?SQL语句或存储过程都可以


[解决办法]
try

Select * From
(select top 10 * from Article where ClassID=1
Union All
select top 10 * from Article where ClassID=2
Union All
select top 10 * from Article where ClassID=3 ) A
[解决办法]
如上
如果忽略重复项
Select * From
(select top 10 * from Article where ClassID=1
Union
select top 10 * from Article where ClassID=2
Union
select top 10 * from Article where ClassID=3 ) A

读书人网 >SQL Server

热点推荐