如何用SQL返回2个表中的所有列,并且数据不要有重复的,如表X
字段A                字段B
2010.01.02            20
2010.01.04            50
2010.01.04            60表Y
字段A                字段B
2010.01.02            100
2010.01.04            90
2010.01.05            900
2010.01.06            120返回数据:字段A                字段B     字段A               字段B
2010.01.02            100     2010.01.02            20   
2010.01.04            90      2010.01.04            50
2010.01.05            900     2010.01.04            60  
2010.01.06            120 

解决方案 »

  1.   


    -- 没有对应关系的两个表关联,用临时表吧。
    select id=identity(int,1,1),* into #lsb1 from 表X
    select id=identity(int,1,1),* into #lsb2 from 表Yselect distinct b.*,a.* 
    from 表X a join 表Y b on a.id=b.id
      

  2.   

    create table t1(a varchar(10),b int)
    go
    insert t1
    select '2010.01.02',0
    union select '2010.01.04', 50
    union select '2010.01.04', 60create table t2(a varchar(10),b int)
    go
    insert t2
      select '2010.01.02', 100
    union select '2010.01.04', 90
    union select '2010.01.05', 900
    union select '2010.01.06', 120select id=identity(int,1,1),* into #lsb1 from t1
    select id=identity(int,1,1),* into #lsb2 from t2select x.a,x.b,y.a,y.b
    from #lsb2 x left join #lsb1 y on x.id=y.iddrop table t1,t2,#lsb1,#lsb2
      

  3.   

    没有关联,我就要这个结果,leo_lesley 的代码正确