有三个表,表结构结构完全一样,相互之间没有关联。
我想同时查询这三个表,并按 time 排序获取前十条数据。
非常感谢!三个表的结构为:  ID      name     time
------  -------  ------------
   1      book    2010-6-10
   2      cat     2010-10-10
   3      apple   2010-7-10
   4      monkey  2010-8-10

解决方案 »

  1.   


    select top 10 *
    from (select * from tb1 
          union all 
          select * from tb2
          union all 
          select * from tb3) a
    order by time 
      

  2.   

    select top 10 *
    from
    (select * from t1 
     union all 
     select * from t2 
     union all 
     select * from t3
    ) t
    order by [time]
      

  3.   

    select top 10 *
    from
    (select * from t1 
     union all 
     select * from t2 
     union all 
     select * from t3
    ) t
    order by [time]
      

  4.   

    select top 10 *
    from
    (select * from t1 
     union all 
     select * from t2 
     union all 
     select * from t3
    ) t
    order by [time]
      

  5.   


    select top (10) * from
    (
       select * from ta1
       union all
       select * from tb2
    )t order by [time]
      

  6.   

    select top 10 *
    from
    (select * from t1 
     union all 
     select * from t2 
     union all 
     select * from t3
    ) t
    order by [time]
      

  7.   

    select top 10 *
    from
    (select *,'t1' as tb from t1 
     union all 
     select *,'t2' as tb from t2 
     union all 
     select *,'t3' as tb from t3
    ) t
    order by [time]
      

  8.   


    drop table #test
    go
    create table #test
    (
    ID  int,
    name  varchar(100),
    time datetime
    )
    goinsert into #test values(1, 'book', '2010-6-10')
    insert into #test values(2,'cat','2010-10-10')
    insert into #test values(3,'apple','2010-7-10')
    insert into #test values(4,'monkey','2010-8-10')
    go
    select * from #test
    go
    select top 10 * from 
    (
    select * from #test   
    union all
    select * from #test   --- 换成表2名称
    union all 
    Select * from #test   ---换成表3名称
    ) a
    order by [time][code=SQL]
    1 book 2010-06-10 00:00:00.000
    1 book 2010-06-10 00:00:00.000
    1 book 2010-06-10 00:00:00.000
    3 apple 2010-07-10 00:00:00.000
    3 apple 2010-07-10 00:00:00.000
    3 apple 2010-07-10 00:00:00.000
    4 monkey 2010-08-10 00:00:00.000
    4 monkey 2010-08-10 00:00:00.000
    4 monkey 2010-08-10 00:00:00.000
    2 cat 2010-10-10 00:00:00.000[/code]
      

  9.   

    select top 10 *
    from
    (select *,'t1' as 'TableName' from t1 
     union all 
     select *,'t2' as 'TableName' from t2 
     union all 
     select *,'t3' as 'TableName' from t3
    ) t
    order by [time]
    默认升序, 如降序 则加上 DESC