MySQL 重叠日期,无冲突

发布于 2024-11-17 16:51:26 字数 553 浏览 9 评论 0原文

我已经看到很多关于 MySQL 中日期范围的线程,但我似乎仍然无法找到我正在寻找的答案,因此任何帮助都将受到极大的欢迎。

我有一个包含 3 列的 MySQL 表,日期 - 开始时间 - 结束时间。日期是MySQL“日期”类型字段,开始和结束时间都是“时间”类型字段。

举例来说,我在数据库中有一个条目,如下所示,我们将此会话称为 1;

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

如果我要添加另一个会话,我需要确保它不会与现有会话冲突。因此以下操作将会失败,因为它位于会话 1 的开始时间和结束时间之间。

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

因此,只能在现有会话“之后”或“之前”插入记录。

我正在使用 PHP/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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

调妓 2024-11-24 16:51:26

我正在使用 PHP/MySQL,并且基于可以运行查询的基础,如果有“有”匹配结果,则失败,如果有“没有”匹配结果,则插入。

试试这个。这里 :date: 是您要添加的条目的日期,:start-time::finish-time: 是分别是其开始和结束时间。

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`

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`
怼怹恏 2024-11-24 16:51:26

我遇到了日期重叠检测的问题,我的第一个想法是做类似 Hammerite 的解决方案,但我发现这个解决方案不完整,主要是因为有太多可能的情况,两个日期范围可能会发生冲突:

我最终用来解决的查询这就像:

/* overlapping dates */
SELECT * 
FROM my_table
WHERE `date` = $date AND
  NOT (
    `start_time` < $start_time and `finish_time` < $start_time
    OR
    `start_time` > $end_time and `finish_time` > $end_time
  )

I faced this problem of date overlapping detection, my first idea was to do something like Hammerite's solution but i found this solution incomplete mainly because there are too many possible scenarios where two date ranges can be in conflict:

The query I ended up using to solve this was something like:

/* overlapping dates */
SELECT * 
FROM my_table
WHERE `date` = $date AND
  NOT (
    `start_time` < $start_time and `finish_time` < $start_time
    OR
    `start_time` > $end_time and `finish_time` > $end_time
  )
淤浪 2024-11-24 16:51:26

我会以不同的方式构建表格。我有两列,均为 datetime 类型,名为 session_startsession_end

逻辑是:如果 session_start 时间不是 >< 而不是旧会话 session_end,则无法插入新会话代码>.

I would structure the table in a different way. I'd have two columns, both datetime type, named session_start and session_end.

Logic is: you cannot insert new session if it's session_start time isn't > or < than old session session_end.

合约呢 2024-11-24 16:51:26

假设 $date、$startTime 和 $finishTime 是 PHP 变量,分别存储要插入的日期、开始时间和结束时间。

$query = 'INSERT INTO `session`
SELECT \'' . $date . '\', \'' . $startTime . '\', \'' . $finishTime . '\'
FROM `session`
WHERE NOT EXISTS (SELECT *
    FROM `session`
    WHERE `date` = \'' . $date . '\'
    AND \'' . $startTime . '\' BETWEEN `startTime` and `finishTime`
)';

希望这有帮助。

Assuming $date, $startTime and $finishTime are your PHP variables that store the date, start time and finish time respectively to be inserted.

$query = 'INSERT INTO `session`
SELECT \'' . $date . '\', \'' . $startTime . '\', \'' . $finishTime . '\'
FROM `session`
WHERE NOT EXISTS (SELECT *
    FROM `session`
    WHERE `date` = \'' . $date . '\'
    AND \'' . $startTime . '\' BETWEEN `startTime` and `finishTime`
)';

Hope this helps.

蔚蓝源自深海 2024-11-24 16:51:26

我会使用简单的:

INSERT INTO session
  ( `date`, startTime, finishTime )
  SELECT
  ( $date, $startTime, $finishTime )
  WHERE NOT EXISTS
    ( SELECT
          *
      FROM
          session
      WHERE
            `date` = $date 
        AND $startTime <  finishTime 
        AND  startTime < $finishTime 

    )

如果您想要两个周期09:00 - 11:00,则应将<更改为<=11:00 - 13:00 发生碰撞。

I would use the simple:

INSERT INTO session
  ( `date`, startTime, finishTime )
  SELECT
  ( $date, $startTime, $finishTime )
  WHERE NOT EXISTS
    ( SELECT
          *
      FROM
          session
      WHERE
            `date` = $date 
        AND $startTime <  finishTime 
        AND  startTime < $finishTime 

    )

The < should be changed to <= if you want the two periods 09:00 - 11:00 and 11:00 - 13:00 to collide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文