两个表均为(time,data)结构,表里面的内容可能各不相同。
要求查询在一段时间内的内容以字段.(time1,data1,time2,data2)显示,可能在不同表中返回的行数不同,以返回结果多的行数为新表的行数,少的那个补NULL。不知道如何实现,请教?
我之前这样写的,发现可能是交叉连接:
select t1.time as time1,t1.data as data1,t2.time as time2,t2.data as data2 from t1,t2 
where (t1.time between :para1 and :para1) and (t2.time between :para1 and :para1).
本来分别查询各个表一次返回30,50条数据,如上的查询却返回的相当多。
谢谢

解决方案 »

  1.   

    --提供個笨方法供參考
    select id=identity(int,1,1),time1,date1 into #t1 from T1 
    where (XXXX)select id=identity(int,1,1),time2,date2 into #t2 from T2
    where  (XXXX)create table #t(id int,time1 varchar(20),date1 varchar(20),time2 varchar(20),date2 varchar(20))if(select max(id) from #t1)>(select max(id) from #t2)
    begin
      insert into #t(id,time1,date1) select id,time1,date1 from #t1
      update #t set time2=#t2.time2,date2=#t2.date2
        from #t2 where #t.id=#t2.id
    end
    else
    begin
     insert into #t(id,time2,date2) select id,time2,date2 from #t2
     update #t set time1=#t1.time1,date1=#t1.date1
        from #t1 where #t.id=#t1.id
    endselect time1,date1,time2,date2 from #tdrop table #t1,#t2,#t