有五个表,分别是t1 t2 t3 t4 t5,查询t1中时间段从0:00到1:00的数据,t2 从1:00到2:00的数据,t3从2:00到3:00的数据,依次类推,t5中4:00到5:00的数据,然后把这些数据汇总到新表t6中,请问如何实现。
我是个数据库菜鸟,一个项目要用到数据库,您能详细的跟我说说吗?

解决方案 »

  1.   

    查询出来的数据列一样嘛?用Union all吧
      

  2.   

    insert into t6
    select *
    from (select * from t1 union all
          select * from t2 union all
          select * from t3 union all
          select * from t4 union all
          select * from t5) b
      

  3.   

    insert into t6(f1,f2,f3,fn)
     select f1,f2,f3,fn
     from t1
     where convert(char(5),时间,114) between '0:00' and '1:00'
     union all
     select f1,f2,f3,fn
     from t2
     where convert(char(5),时间,114) between '1:00' and '2:00'
      

  4.   


    select * into t6 from
    (
      select * from t1
      union all
      select * from t2
      union all
      select * from t3
      union all
      select * from t4
      union all
      select * from t5
    ) t
      

  5.   

    insert into t6
    select * from t1 union all
          select * from t2 union all
          select * from t3 union all
          select * from t4 union all
          select * from t5
      

  6.   

    insert into t6(f1,f2,f3,fn)
    select *
    from
    (
     select f1,f2,f3,fn
     from t1
     where convert(char(5),时间,114) between '0:00' and '1:00'
     union all
     select f1,f2,f3,fn
     from t2
     where convert(char(5),时间,114) between '1:00' and '2:00'
    union all
     select f1,f2,f3,fn
     from t3
     where convert(char(5),时间,114) between '2:00' and '3:00'
    union all
     select f1,f2,f3,fn
     from t4
     where convert(char(5),时间,114) between '3:00' and '4:00'
    union all
     select f1,f2,f3,fn
     from t5
     where convert(char(5),时间,114) between '4:00' and '5:00'
    ) t
      

  7.   

    select * into t6(f1,f2,f3,fn)
    from
    (
     select f1,f2,f3,fn
     from t1
     where convert(char(5),时间,114) between '0:00' and '1:00'
     union all
     select f1,f2,f3,fn
     from t2
     where convert(char(5),时间,114) between '1:00' and '2:00'
    union all
     select f1,f2,f3,fn
     from t3
     where convert(char(5),时间,114) between '2:00' and '3:00'
    union all
     select f1,f2,f3,fn
     from t4
     where convert(char(5),时间,114) between '3:00' and '4:00'
    union all
     select f1,f2,f3,fn
     from t5
     where convert(char(5),时间,114) between '4:00' and '5:00'
    ) t
      

  8.   

    Select * Into T6 From
    (
    Select 字段1 From T1
    union all 
    Select 字段1 From T2
    union all
    Select 字段1 From T3
    union all
    Select 字段1 From T4
    union all
    Select 字段1 From T5
    )M
      

  9.   

    请问f1 f2 f3 这些是什么,表格里面的列吗?
      

  10.   

    insert into t6
    select *
    from (select * from t1 union all
          select * from t2 union all
          select * from t3 union all
          select * from t4 union all
          select * from t5) b
    where datacol = ??
      

  11.   

    很感谢您的帮助……
    我试试……
    呵呵,俺是SQL的门外汉……见笑了
      

  12.   

    如果我的时间是具体的2008年3月21日0:00到2008年3月21日1:00的话,前面的convert(char(5),时间,114)是不是要做修改?
      

  13.   

    between '1:00' and '2:00' 
    包括了
      

  14.   

    查询结果如果字段相同,用union 或union all