日期:2014-05-18  浏览次数:20661 次

如何将查询结果合并
有两条sql语句
select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES where DISPLAY=1

select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES

如何将这两条语句查询结果合并(相同的就合并)

------解决方案--------------------
SQL code

select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES where DISPLAY=1
union  --去掉两个查询的重复行,重复行显示一次
select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES

------解决方案--------------------
SQL code


select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES
;with tmp as (
select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES where DISPLAY=1
)
select NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME from tmp as a
where a.NOTICENO not in (select top 3 NOTICENO from NOTICES)
union all
select top 3 NOTICENO,NT_TITLE,NT_CONTENT,USERNAME,ADDTTIME
from NOTICES