select *
from (
select *,1 as oid
from tb where --sql1
union all 
select *,2 as oid
from tb where --sql2)a
order by oid

解决方案 »

  1.   


    create table tb(id int)insert into tb
    select 2 union all
    select 1 union all
    select 3select * 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
    */
      

  2.   


    select * from TB where.... --SQL1
    union all
    select * from TB where.... --SQL2 and not exists(select * from TB where.... --SQL1+條件)
      

  3.   

    1楼和2楼是对两个查询添加一个字段,然后按照这个字段进行排序,懂了。3楼的方法会进行尝试,但因为where条件语句很长,不知道可不可行。谢谢各位。