table a(id)     table b(id)
id              id
1               3
2               4
                5想要得到如下
id1     id2
1       3
2       4
null    5

解决方案 »

  1.   

    select id1 = a.id,id2 = b.id from b left join a
      

  2.   

    select * from tableA as a right join tableB as b on a.id = b.id
      

  3.   

    declare @ta table(id int)
    declare @tb table(id int)
    insert @ta
    select 1 union all 
    select 2
    insert @tb
    select 3 union all
    select 4 union all
    select 5----查询(注意:每个表的id必须是递增的)
    select a.id,b.id from @tb as b left join @ta as a 
    on (select count(*) from @ta where id < a.id) = (select count(*) from @tb where id < b.id)/*结果
    id          id          
    ----------- ----------- 
    1           3
    2           4
    NULL        5
    */
      

  4.   

    --不知道楼主的数据是有意还是无意
    select * from #a a
    right join
    #b b
    on b.id-a.id=2
      

  5.   

    select id1 = a.id,id2 = b.id from b right join a
      

  6.   

    SELECT a.id, b.id FROM b LEFT OUTER JOIN
          a ON b.id - a.id = 2
      

  7.   

    你可以在2个表中增加一个identity()列,然后进行连接
      

  8.   

    select a.*,b.*
    from 
    (select row_number() over (order by id) as Recn,* from t1) as a
    left outer join
    (select row_number() over (order by id) as Recn,* from t2) as b
    on a.recn=b.recn
      

  9.   

    TO:  hellowork(一两清风) ( )
    on (select count(*) from @ta where id < a.id) = (select count(*) from @tb where id < b.id) 这么牛鼻的 条件你 都 想得出来 ,真牛鼻