数据库里有“开始时间” a 和“结束时间” b ,现再给一个“开始时间” a1 和“结束时间” b1
现在要判断a1至b1时间段里是否 a 至 b 之间的日期
例:a = 2010-05-01 , b = 2010-06-30
    a1 = 2010-06-01 , b1 = 2010-07-31
现在的日期里可以看出,从2010-06-01至2010-06-30这一期间里是重复的,我只需要判断出a到b这段时间是有重复,而不需要判断具体的重复时间段。
可以查出有几条记录重复即可。

解决方案 »

  1.   

    mysql> insert into t5 values(1, '2010-05-01', '2010-06-30');
    Query OK, 1 row affected (0.00 sec)mysql> insert into t5 values(2, '2010-06-05', '2010-06-25');
    Query OK, 1 row affected (0.00 sec)mysql> select count(*) from t5 where (a>'2010-06-01' and a<'2010-07-31') or (b>'2010-06-01' and b < '2010-07-31');
    +----------+
    | count(*) |
    +----------+
    |        2 |
    +----------+
    1 row in set (0.03 sec)mysql>
      

  2.   

    select *
    from table1 
    where a<b1 and b>a1就这么简单。
      

  3.   

    这个只是一种情况,如果提供的日期是 a2 和 b2又怎么办?还有其它的一些情况如a3,b3呢。
    a = 2010-05-01 , b = 2010-06-30
    a1 = 2010-06-01 , b1 = 2010-07-31
    a2 = 2010-04-01 , b2 = 2010-07-31
    a3 = 2010-05-10 , b3 = 2010-08-31
      

  4.   

    select *
    from table1 
    where a<b1 and b>a1这两个不是变量吗?你自己程序中换就是了。
      

  5.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  6.   

    CREATE TABLE `NewTable` (
    `id`  int(11) NOT NULL AUTO_INCREMENT ,
    `startdate`  date NULL DEFAULT NULL ,
    `enddate`  date NULL DEFAULT NULL ,
    PRIMARY KEY (`id`)
    )insert into datetest (startdate,enddate)  values ('2010-03-01','2010-05-31')
    insert into datetest (startdate,enddate) values ('2010-06-01','2010-07-31')
    insert into datetest (startdate,enddate) values ('2010-08-01','2010-08-31')现根据一组日期进行判断,如:2010-04-01 -- 2010-06-20 这段时间内有没有和数据库中的时间段有重复
    如:2010-04-01 -- 2010-05-31 这段时间就和第一条记录重复因为我要在录入数据时判断时间段内不允许出现重复,不知道这样表达能看明白吗?
      

  7.   


    我已经回答的很清楚了。每次select count(*), 如果大于0,就有重复。如果等于0,则有效。
    条件里不是已经给出了吗?
      

  8.   


    你好,你的回答只能满足部份条件,如果是
    mysql> select count(*) from t5 where (a>'2010-06-01' and a<'2010-07-31') or (b>'2010-06-01' and b < '2010-06-15');这样的话就不能识别了
      

  9.   

    iihero 你好,我在你的基础上再加了一个判断
    select count(*) from t5 where (a>'2010-06-01' and a<'2010-07-31') or (b>'2010-06-01' and b < '2010-06-15') or (a<'2010-06-01' and b>'2010-07-31');看上去应该是可以了,先试一试
      

  10.   

    select *
    from table1  
    where a<b1 and b>a1

    利用这个,直接可以查出有时间重叠的记录。mysql> select * from NewTable;
    +----+------------+------------+
    | id | startdate  | enddate    |
    +----+------------+------------+
    |  1 | 2010-03-01 | 2010-05-31 |
    |  2 | 2010-06-01 | 2010-07-31 |
    |  3 | 2010-08-01 | 2010-08-31 |
    +----+------------+------------+
    3 rows in set (0.00 sec)mysql> select * from NewTable where startdate<'2010-06-20' and enddate>'2010-04-01';
    +----+------------+------------+
    | id | startdate  | enddate    |
    +----+------------+------------+
    |  1 | 2010-03-01 | 2010-05-31 |
    |  2 | 2010-06-01 | 2010-07-31 |
    +----+------------+------------+
    2 rows in set (0.00 sec)mysql>