首先,感谢大家的支持。
终于得到了一颗钻石。已经比 大版 十豆三 老师预计了晚了近一个星期的时间才收获到。CSDN比去年来说,MYSQL版出现了许多新的声音,新的面孔,特别是很多来自MS SQL SERVER, ORACLE的数据库方面的专家朋友的积极参与。使用我们在共同的讨论,争论,辩论中不断提高。 在这儿再一次感谢大家的参考和支持。原本应该在拿到钻的当时就应该按CSDN的惯例放分的,可是一直想不出放个什么小问题。所以拖了好几天,今天刚巧看到这个经常出现的问题。所以提出来以供大家分享参考。===============================================================================比如某个会议室的预订信息表 roomBookInfo(id,title,beginTime,endTime), 现在有一个新的预订请求 @bTime, @eTIme 分别 代表 开始、结束时间。请检查下表中的记录,查看是否有时间上的冲突。create table roomBookInfo (id int auto_increment primary key, title varchar(10), beginTime datetime not null , endTime datetime not null);===============================================================================最后预祝 zuoxingyu 升星

解决方案 »

  1.   

    select * 
    from roomBookInfo A
    where exists(select 1 from roomBookInfo B 
    where ( A.begintime>B.begintime and A.begintime<B.endtime )
      

  2.   

    虽然我很菜,但是还是想试试:表结构:-- 
    -- 表的结构 `roomBookInfo`
    -- 
    -- 创建时间: 2010 年 10 月 15 日 00:01
    -- 最后更新时间: 2010 年 10 月 15 日 00:08
    -- DROP TABLE IF EXISTS `roomBookInfo`;
    CREATE TABLE IF NOT EXISTS `roombookinfo` (
      `id` int(4) unsigned NOT NULL auto_increment,
      `title` varchar(10) NOT NULL,
      `beginTime` datetime NOT NULL,
      `endTime` datetime NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=gbk AUTO_INCREMENT=4 ;-- 
    -- 导出表中的数据 `roomBookInfo`
    -- INSERT INTO `roomBookInfo` VALUES (1, '会议一', '2010-10-15 00:07:58', '2010-10-15 00:09:06');
    INSERT INTO `roomBookInfo` VALUES (2, '会议二', '2010-10-15 00:08:27', '2010-10-15 00:08:33');
    INSERT INTO `roomBookInfo` VALUES (3, '会议三', '2010-10-16 00:08:41', '2010-10-17 00:08:44');SQL语句:SELECT m.*
    FROM roombookinfo m
    LEFT JOIN roombookinfo s ON m.beginTime > s.endTime OR m.endTime < s.beginTime
         WHERE m.beginTime >= @eTime OR m.endTime <= @bTime
    貌似有错误呃。
      

  3.   


    不知道行不行,刚刚改了下:SELECT 
           CASE (SELECT COUNT(*) FROM roombookinfo WHERE beginTime <= @bTime AND endTime >= @eTime )
                WHEN 0 THEN '会议未创建'
                WHEN 1 THEN '有一个会议'
           ELSE
               '有很多重复的会议'
           END
      

  4.   

    最好贴记录出来看看,时间上的冲突有不同的理解,
    一般判断BEGINTIME是否>ENDTIME,1个自连接查询
      

  5.   

    找出与 (@btime ,@etime) 有交集的记录,
    not (begintime >= @etime or EndTime <= @btime)也即是 begintime<@etime and Endtime>@btime
      

  6.   

    select *
    from roomBookInfo
    where beginTime<@eTime
          and endTime>@bTime;
      

  7.   

    很原始的检查方法select * from roombookinfo where (begintime BETWEEN @btime and @etime) or (endtime BETWEEN @btime and @etime) or (@btime between begintime and endtime) or (@etime BETWEEN begintime and endtime);
      

  8.   

    借狼头大哥的帖子问一个问题(类似的):由于某种原因,在insert的时候,没有进行时间冲突判断,过后需要判断某段时间是否有冲突,如何判断?比如存在两条记录,一条是9:00--11:00,一条是10:00--12:00
    这样的话,如果我查9:00--12:00这个时间段内是否有冲突,就应该显示有冲突。谢谢。
      

  9.   

    09-11
    10-12
    就是判断ENDTIME>BEGINTIME,11>10,存在冲突
      

  10.   

    要分两步:
    1、是时间表本身是否有冲突,

    09-11
    10-12
    就是判断ENDTIME>BEGINTIME,11>10,存在冲突
    2、
    10:15--10:45,这样查询出来有两个结果
    09-11
    10-12
    如果你的要求只要1个结果,分组求COUNT(*)>1就可以查出有冲突
      

  11.   


    CREATE TABLE  test (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `begintime` datetime NOT NULL,
      `endtime` datetime NOT NULL,
      PRIMARY KEY (`id`)
    ) ;insert into test(begintime,endtime) values
     ('2010-10-15 09:00:00','2010-10-15 11:00:00')
    ,('2010-10-15 10:00:00','2010-10-15 12:00:00')
    ,('2010-10-16 10:00:00','2010-10-16 12:00:00')
    ,('2010-10-17 10:00:00','2010-10-17 12:00:00')
    查9:00--12:00是否有冲突,结果为有
    查9:00--9:30 是否有冲突,结果为没有
      

  12.   

    比如某个会议室的预订信息表 roomBookInfo(id,title,beginTime,endTime), 现在有一个新的预订请求 @bTime, @eTIme 分别 代表 开始、结束时间。请检查下表中的记录,查看是否有时间上的冲突。create table roomBookInfo (id int auto_increment primary key, title varchar(10), beginTime datetime not null , endTime datetime not null);
      

  13.   

    SQL有问题,只能精确到分钟,不能到秒!
    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type in (N'U'))
    DROP TABLE [dbo].test
    GO
    CREATE TABLE  test (
      id int NOT NULL,
      title varchar(50) NULL,
      begintime datetime NOT NULL,
      endtime datetime NOT NULL,
    )
    INSERT INTO test VALUES (1, '会议一', '2010-10-15 09:00:01', '2010-10-15 09:30:00');
    INSERT INTO test VALUES (2, '会议二', '2010-10-15 10:08:27', '2010-10-15 10:08:33');
    INSERT INTO test VALUES (3, '会议三', '2010-10-16 15:08:41', '2010-10-17 16:08:44');
    go
    declare @btime smalldatetime,@etime smalldatetime
    select @btime='2010-10-15 08:01:00'
    select @etime='2010-10-15 09:00:30'
    select * from test where ((@bTime>=beginTime and @bTime<endTime) or (@bTime<endTime and @eTIme>endTime) or (@bTime<=beginTime and @eTIme>beginTime))
    go
      

  14.   


    declare @btime smalldatetime,@etime smalldatetime
    select @btime='2010-10-15 08:01:00'
    select @etime='2010-10-15 09:00:10'
    select * from test where ((@bTime>=beginTime and @bTime<endTime) or (@bTime<endTime and @eTIme>endTime) or (@bTime<=beginTime and @eTIme>beginTime))
    /*
    id title begintime endtime
    */
    select @btime='2010-10-15 08:01:00'
    select @etime='2010-10-15 09:00:30'
    select * from test where ((@bTime>=beginTime and @bTime<endTime) or (@bTime<endTime and @eTIme>endTime) or (@bTime<=beginTime and @eTIme>beginTime))
    /*
    id title begintime endtime
    1 会议一 2010-10-15 09:00:01.000 2010-10-15 09:30:00.000
    */
    select @btime='2010-10-15 09:01:00'
    select @etime='2010-10-15 09:10:10'
    select * from test where ((@bTime>=beginTime and @bTime<endTime) or (@bTime<endTime and @eTIme>endTime) or (@bTime<=beginTime and @eTIme>beginTime))
    /*
    id title begintime endtime
    1 会议一 2010-10-15 09:00:01.000 2010-10-15 09:30:00.000
    */
    select @btime='2010-10-15 09:01:00'
    select @etime='2010-10-15 10:10:10'
    select * from test where ((@bTime>=beginTime and @bTime<endTime) or (@bTime<endTime and @eTIme>endTime) or (@bTime<=beginTime and @eTIme>beginTime))
    /*
    id title begintime endtime
    1 会议一 2010-10-15 09:00:01.000 2010-10-15 09:30:00.000
    2 会议二 2010-10-15 10:08:27.000 2010-10-15 10:08:33.000
    */
    go
      

  15.   

    用sql语句能实现真是想不出来类似问题我都是写程序循环判断了。
      

  16.   

    用not exists吧 ,排除掉正常的,剩下的就是有冲突的了
     select *  from roomBookInfo where not exists( select * from roomBookInfo where beginTime >= endTime)
      

  17.   

    SET @n1='2010-10-15 09:00:00';
    SET @n2='2010-10-15 12:30:00';
    SELECT * FROM test a
    WHERE (CAST(@n1 AS TIME)>=TIME(begintime) AND TIME(endtime)<=CAST(@n2 AS TIME)
    OR
     CAST(@n2 AS TIME)>=TIME(begintime) )
     AND DATE_FORMAT(begintime,'%Y-%m-%d')=DATE_FORMAT(@n1,'%Y-%m-%d')
      

  18.   

    这是逻辑题申请时间start-----end查找冲突。查找所有: 开始时间<end and 结速时间>start 所有记录当记录存在时则有冲突