当前位置: 代码迷 >> Sql Server >> 怎么合并同一表中通过不同查询条件产生的结果集
  详细解决方案

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

热度:70   发布时间:2016-04-27 15:50:54.0
如何合并同一表中通过不同查询条件产生的结果集
如:
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
  相关解决方案