数据库某张表里有两个字段:开始时间和结束时间,两个时间组成一个时间区域(开始时间要小于结束时间)。
 我现在想查一段时间范围内的数据,凡是和这段范围重叠的数据都要查出来。
例如:
我要查某月11号到20号的数据数据1:8--15
数据2: 7--10
数据3:15--28
数据3:13--16
数据5:21--24
数据6:20--24上面这些数据除了“数据5”和“数据2”不符合要求以为其他的都符合,所以要求就是只要时间范围重叠,都符合。急,在线等。

解决方案 »

  1.   

    我就是想知道查询条件怎么写,
    这两个字段名分别是begindate和enddate,数据类型是datetime类型的
     只要能查出范围重叠的数据的条件都行
      

  2.   

    create table tb(name varchar(10), data1 int, data2 int)
    insert into tb values('数据1',8 ,15) 
    insert into tb values('数据2',7 ,10) 
    insert into tb values('数据3',15,28) 
    insert into tb values('数据4',13,16) 
    insert into tb values('数据5',21,24) 
    insert into tb values('数据6',20,24)
    go
    --临时表
    select top 8000 identity(int,0,1) as id into # from syscolumns a,syscolumns bselect distinct name from 
    (
      select name , data1 + n.id data from tb , # n where data1 + n.id <= data2
    ) t
    where data between 11 and 20
    order by namedrop table tb , #5/*
    name       
    ---------- 
    数据1
    数据3
    数据4
    数据6(所影响的行数为 4 行)
    */
      

  3.   

    create table tb([data] varchar(10), time1 int, time2 int)
    insert into tb values('数据1',8 ,15) 
    insert into tb values('数据2',7 ,10) 
    insert into tb values('数据3',15,28) 
    insert into tb values('数据4',13,16) 
    insert into tb values('数据5',21,24) 
    insert into tb values('数据6',20,24)
    go
    select [date] from tb where (time1 between 11 and 20) or (time2 between 11 and 20)