34,588
社区成员
发帖
与我相关
我的任务
分享
select * from TB where.... --SQL1
select * from TB where.... --SQL2
select * from TB where.... --SQL1
union all
select * from TB where.... --SQL2 and not exists(select * from TB where.... --SQL1+條件)
create table tb(id int)
insert into tb
select 2 union all
select 1 union all
select 3
select * from tb
/*
id
2
1
3
*/
--在去重后,仍然保持原来的顺序
select id
from
(
select *,
ROW_NUMBER() over(order by getdate()) as rownum
from tb
union
select *,
ROW_NUMBER() over(order by getdate()) as rownum
from tb
)t
order by rownum
/*
2
1
3
*/