dop2144 2011-06-29 10:47
浏览 198
已采纳

MySQL重叠日期,没有冲突

I've seen lots of threads about date ranges in MySQL but I still don't seem to be able to find an answer for what I'm looking for so any help will be greatly received.

I have a MySQL table with 3 columns, date - startTime - finishTime. The date is a MySQL 'date' type field and the start and finish times are both 'time' type fields.

Say for example I have an entry in the database as follows, lets call this session 1;

date = 2011-06-30, startTime = 09:00:00, finishTime = 11:00:00

If I come to add another session I need to make sure that it doesn't conflict with an existing session. So the following would fail because it falls in between session 1 start and finish times.

date = 2011-06-30, startTime = 10:00:00, finishTime = 12:00:00

So the record can only be inserted 'AFTER' or 'BEFORE' an existing session.

I'm using PHP/MySQL and am going on the basis that a query can be run and if there 'are' matching results then, fail, if there 'arent' matching results then insert.

Thanks in advance.

  • 写回答

5条回答 默认 最新

  • dougou7782 2011-06-29 11:03
    关注

    I'm using PHP/MySQL and am going on the basis that a query can be run and if there 'are' matching results then, fail, if there 'arent' matching results then insert.

    Well, try this. Here :date: is the date of the entry you are going to add, and :start-time: and :finish-time: are its start and finish times respectively.

    SELECT EXISTS (
        SELECT
            1
        FROM
            TableName
        WHERE
            `date` = :date: AND
            ( :start-time: BETWEEN startTime AND finishTime OR
              :finish-time: BETWEEN startTime AND finishTime OR
              startTime BETWEEN :start-time: AND :finish-time:
              )
    ) AS `Clash`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化