表A
字段如下:
ID  用户  开始时间     结束时间     数据



表B
字段如下:
ID  用户  日期时间   数据1  数据2   数据3 
。。
。。
。。
现在怎么先从表A中查询出开始时间和结束时间,然后根据开始时间和结束时间再从表B中查询这段时间内的数据呢?
即就是根据表A中的开始时间和结束时间查询表B中时间段内的数据.
怎么样用一句SQL语句完成呢?还请大家多多帮忙,谢谢!
 

解决方案 »

  1.   

    SELECT * FROM TABLE A LEFT JOIN TABLE B ON B.日期时间 BETWEEN A.开始时间 AND A.结束时间
      

  2.   

    字段如下:
    ID  用户  开始时间              结束时间                  数据
    1   a     2009-07-14 08:01:00   2009-07-14 08:09:00        2
    2   b     2009-07-14 09:01:00   2009-07-14 09:09:00        2
    3   a     2009-07-14 09:01:00   2009-07-14 09:09:00        3表B
    字段如下:
    ID  用户  日期时间               数据1  数据2  数据3
    1   a     2009-07-14 08:01:00     1       1     1
    2   a     2009-07-14 08:02:00     3       2     3
    3   a     2009-07-14 08:03:00     3       2     5
    4   a     2009-07-14 08:05:00     5       2     5
    3   a     2009-07-14 09:03:00     3       2     5
    最后查询结果应该是(就拿表A中用户a的第一条记录说)
    1    1     1
    3    2     3
    3    2     5
    5    2     5
      

  3.   

    select a.*,b.* from a left join b on b.riqi between a.kaishi and a.jiesu
      

  4.   

    select b.数据1,b.数据2, b.数据3 from b inner join a where a.ID=b.ID and b.日期时间 between a.开始时间 and a.结束时间
      

  5.   

    select distinct 数据1,数据2,数据3 
    from A,B
    where 日期时间 between A.开始时间 and A.结束时间
      

  6.   

    -- =========================================
    -- -----------t_mac 小编-------------
       ---希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb1') IS NOT NULL
      DROP TABLE tb1
    GO
    CREATE TABLE tb1(ID int , 用户 char(1) ,开始时间 datetime  , 结束时间   datetime, 数据 int )
    go
    insert into tb1
    select  
    1 , 'a'  ,  '2009-07-14 08:01:00' , '2009-07-14 08:09:00'   ,     2 union all select 
    2  ,'b'  ,  '2009-07-14 09:01:00'  ,'2009-07-14 09:09:00'   ,     2 union all select 
    3 , 'a'  ,  '2009-07-14 09:01:00'  ,'2009-07-14 09:09:00'  ,      3 goIF OBJECT_ID('tb2') IS NOT NULL
      DROP TABLE tb2
    GO
    CREATE TABLE tb2(ID int , 用户 char(1) ,日期时间 datetime  , 数据1 int,数据2 int,数据3 int )
    go
    insert into tb2
    select  
    1,  'a' ,   '2009-07-14 08:01:00'  ,  1,     1   , 1 union all select 
    2,  'a',    '2009-07-14 08:02:00'  ,  3 ,     2 ,   3 union all select 
    3,  'a' ,   '2009-07-14 08:03:00' ,   3 ,     2  ,  5 union all select 
    4,  'a' ,   '2009-07-14 08:05:00' ,   5  ,    2  ,  5 union all select 
    5,  'a' ,   '2009-07-14 09:03:00'  ,  3  ,    2  ,  5 go
    select 数据1,数据2,数据3
    from tb1 join tb2 on tb1.用户=tb2.用户
    where 日期时间 between  开始时间 and 结束时间
    /*------------
    (5 行受影响)
    数据1         数据2         数据3
    ----------- ----------- -----------
    1           1           1
    3           2           3
    3           2           5
    5           2           5
    3           2           5(5 行受影响)-------*/
      

  7.   

    select 数据1,数据2,数据3
    from tb1 join tb2 on tb1.用户=tb2.用户
    where 日期时间 between  开始时间 and 结束时间 and tb1.用户='a'
      

  8.   

    select table2.数据1 , table2.数据2 , table2.数据3 from table2 right join table1 on table2.用户=table1.用户 where table2.时间 between table1.开始时间 and table1.结束时间 and table1.id ='a'====================
    你的条件不够明了!你怎么找到表A中的第一条记录?要知道A中id为a的上面就有两条。
      

  9.   


    select b.数据1,b.数据2,b.数据3 from b
    right join a
    on b.用户=a.用户
    where b.日期时间 between  a.开始时间 and a.结束时间