问题如下:
表1数据
ID    日期时间    字段1
1234  2009-08-27  32.2
1235  2009-08-27  33.4
1236  2009-08-27  42.2表2数据
ID    日期时间    字段1  字段2
3234  2009-08-27  32.4   aaa
3235  2009-08-27  53.6   bbb
4236  2009-08-27  12.5   ddd
根据时间查询 2009-08-27  显示结果如下:
ID    日期时间    字段1   ID    日期时间    字段1  字段2
1234  2009-08-27  32.2    3234  2009-08-27  32.4   aaa
1235  2009-08-27  33.4    3235  2009-08-27  53.6   bbb
1236  2009-08-27  42.2    4236  2009-08-27  12.5   ddd
请问大家 SQL 怎么写呀 谢谢了  

解决方案 »

  1.   

    看着没有关联..
    两边数据条数是否一样多..
    如果一样..Union一下..
    select * from 表1 where 日期时间='2009-08-27'
    union all
    select * from 表2 where 日期时间='2009-08-27'
      

  2.   

    union all 
    肯定是不对的。
    两张表都没有主外键的,怎么搞。。
      

  3.   


    如果两个表建的时候就没有关联..
    在相同情况下插入的两个表..
    比如一个表单上要插入两个表..但两个表建的时候没有关联..执行一个表单..表1和表2都新增一条数据..我说的是在两个表记录条数相同的前提下.. union all没有问题..
      

  4.   

    select * from 表1数据,表2数据  where 表1数据.ID=表2数据.ID
      

  5.   


    --思路:将表1中的日期为'2009-08-27'的查询出来,并加上一列序号列.同样将表2中的日期为'2009-08-27'的查询出来,并加上一列序号列.再用一个外连接将两个查询连接起来,即为所求.----表1中的日期为'2009-08-27'的查询--
    SELECT 序号= (SELECT COUNT(ID) FROM 表1 AS a WHERE a.ID <= b.ID and a.日期时间 = '2009-08-27'),b.ID,b.日期时间,b.字段1 FROM 表1 AS b where b.日期时间 = '2009-08-27'--表2中的日期为'2009-08-27'的查询--
    SELECT 序号= (SELECT COUNT(ID) FROM 表2 AS c WHERE c.ID <= d.ID and c.日期时间 = '2009-08-27'),d.ID,d.日期时间,d.字段1,d.字段2 FROM 表2 AS d where d.日期时间 = '2009-08-27'--最后所求--
    select * from (SELECT 序号= (SELECT COUNT(ID) FROM 表1 AS a WHERE a.ID <= b.ID and a.日期时间 = '2009-08-27'),b.ID,b.日期时间,b.字段1 FROM 表1 AS b where b.日期时间 = '2009-08-27') as tab1 left join (SELECT 序号= (SELECT COUNT(ID) FROM 表2 AS c WHERE c.ID <= d.ID and c.日期时间 = '2009-08-27'),d.ID,d.日期时间,d.字段1,d.字段2 FROM 表2 AS d where d.日期时间 = '2009-08-27') as tab2 on tab1.序号 = tab2.序号
      

  6.   

    将上面的left join改为full join吧.
    select tab1.ID,tab1.日期时间.....可以让序号列不显示出来.
      

  7.   


    create table test10 ([ID] int ,[日期时间]  varchar(10) ,[字段1]  varchar(10) ) 
    insert into test10
    select 1234,'2009-08-27', '32.2' 
    union all
    select 1235,'2009-08-27', '33.4' 
    union all
    select 1236,'2009-08-27','42.2'create table test11([ID] int,[日期时间] varchar(10) ,[字段1]  varchar(10) ,[字段2]  varchar(10) ) 
    insert into test11
    select 3234,'2009-08-27','32.4','aaa' 
    union all
    select 3235,'2009-08-27','53.6','bbb'
    union all
    select 4236,'2009-08-27','12.5','ddd' 
    select (select count(*) from test10 a where a.ID <= b.ID ) as iIndex,* 
    into #table1
    from test10 bselect (select count(*) from test11 a where a.ID <= b.ID ) as iIndex,* 
    into #table2
    from test11 bselect a.ID,a.[日期时间],a.[字段1],b.ID,b.[日期时间],b.[字段1],b.[字段2] from #table1 a
    full join #table2 b
    on a.iIndex=b.iIndexdrop table test10
    drop table test11
    drop table #table1
    drop table #table2
    /*
    ID          日期时间       字段1        ID          日期时间       字段1        字段2        
    ----------- ---------- ---------- ----------- ---------- ---------- ---------- 
    1234        2009-08-27 32.2       3234        2009-08-27 32.4       aaa
    1235        2009-08-27 33.4       3235        2009-08-27 53.6       bbb
    1236        2009-08-27 42.2       4236        2009-08-27 12.5       ddd*/
      

  8.   


    select a.ID,a.[日期时间],a.[字段1],b.ID,b.[日期时间],b.[字段1],b.[字段2] from #table1 a,#table2 b where a.iIndex=b.iIndex
    结果一致
      

  9.   

    select  A.ID,A.日期时间,A.字段1 ,B.ID,B.日期时间,B.字段1,B.字段2
    from 表1 A,表2 B
    WHEER A.日期时间=B.日期时间 AND B.日期时间='2009-8-27'
      

  10.   

    if object_id('tempdb..#1') is not null
    drop table #1
    create table #1([ID] int ,[日期时间]  varchar(10) ,[字段1]  varchar(10))
    insert into #1
    select 1234,'2009-08-27', '32.2' 
    union all
    select 1235,'2009-08-27', '33.4' 
    union all
    select 1236,'2009-08-27','42.2'if object_id('tempdb..#2') is not null
    drop table #2
    create table #2([ID] int,[日期时间] varchar(10) ,[字段1]  varchar(10) ,[字段2]  varchar(10) ) 
    insert into #2
    select 3234,'2009-08-27','32.4','aaa' 
    union all
    select 3235,'2009-08-27','53.6','bbb'
    union all
    select 4236,'2009-08-27','12.5','ddd' 
    alter table #1 add id1 int identity(1,1)  
    alter table #2 add id1 int identity(1,1)  
    go
    --select * from #1 a, #2 b where a.id1 = b.id1
    select * from #1 inner join #2 on #1.id1 = #2.id1 
    ID          日期时间       字段1        id1         ID          日期时间       字段1        字段2        id1         
    ----------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- ----------- 
    1234        2009-08-27 32.2       1           3234        2009-08-27 32.4       aaa        1
    1235        2009-08-27 33.4       2           3235        2009-08-27 53.6       bbb        2
    1236        2009-08-27 42.2       3           4236        2009-08-27 12.5       ddd        3(所影响的行数为 3 行)