1. <tfoot id='sAKlj'></tfoot>

  2. <legend id='sAKlj'><style id='sAKlj'><dir id='sAKlj'><q id='sAKlj'></q></dir></style></legend>

        <bdo id='sAKlj'></bdo><ul id='sAKlj'></ul>
    1. <small id='sAKlj'></small><noframes id='sAKlj'>

      <i id='sAKlj'><tr id='sAKlj'><dt id='sAKlj'><q id='sAKlj'><span id='sAKlj'><b id='sAKlj'><form id='sAKlj'><ins id='sAKlj'></ins><ul id='sAKlj'></ul><sub id='sAKlj'></sub></form><legend id='sAKlj'></legend><bdo id='sAKlj'><pre id='sAKlj'><center id='sAKlj'></center></pre></bdo></b><th id='sAKlj'></th></span></q></dt></tr></i><div id='sAKlj'><tfoot id='sAKlj'></tfoot><dl id='sAKlj'><fieldset id='sAKlj'></fieldset></dl></div>

    2. 检查表的时间重叠?

      时间:2023-06-02

      <small id='D0SLY'></small><noframes id='D0SLY'>

        1. <i id='D0SLY'><tr id='D0SLY'><dt id='D0SLY'><q id='D0SLY'><span id='D0SLY'><b id='D0SLY'><form id='D0SLY'><ins id='D0SLY'></ins><ul id='D0SLY'></ul><sub id='D0SLY'></sub></form><legend id='D0SLY'></legend><bdo id='D0SLY'><pre id='D0SLY'><center id='D0SLY'></center></pre></bdo></b><th id='D0SLY'></th></span></q></dt></tr></i><div id='D0SLY'><tfoot id='D0SLY'></tfoot><dl id='D0SLY'><fieldset id='D0SLY'></fieldset></dl></div>
            <tbody id='D0SLY'></tbody>

          • <legend id='D0SLY'><style id='D0SLY'><dir id='D0SLY'><q id='D0SLY'></q></dir></style></legend>

                <bdo id='D0SLY'></bdo><ul id='D0SLY'></ul>
              • <tfoot id='D0SLY'></tfoot>
              • 本文介绍了检查表的时间重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个包含以下字段的 MySQL 表:

                I have a MySQL table with the following fields:

                • 姓名
                • 开始时间
                • 结束时间

                starttimeendtime 是 MySQL TIME 字段(不是 DATETIME).我需要一种方法来定期扫描"表格以查看表格内的时间范围是否有任何重叠.如果有一个来自 10:00-11:00 的事件和另一个来自 10:30-11:30 的事件,我想收到时间重叠的警报.

                starttime and endtime are MySQL TIME fields (not DATETIME). I need a way to periodically "scan" the table to see if there are any overlaps in time ranges within the table. If there is an event from 10:00-11:00 and another from 10:30-11:30, I want to be alerted of the presence of the time overlap.

                没什么特别的,我只想知道是否存在重叠.

                Nothing fancy really, all I want to know whether an overlap exists or not.

                我将使用 PHP 来执行此操作.

                I'm going to be using PHP to execute this.

                推荐答案

                这是一个我多年前找到答案的查询模式:

                This is a query pattern for which I found the answer many years ago:

                SELECT *
                FROM mytable a
                JOIN mytable b on a.starttime <= b.endtime
                    and a.endtime >= b.starttime
                    and a.name != b.name; -- ideally, this would compare a "key" column, eg id
                

                要找到任何重叠",您可以将时间范围的相反两端相互比较.我不得不拿出笔和纸来绘制相邻的范围,才能意识到边缘情况归结为这种比较.

                To find "any overlap", you compare the opposite ends of the timeframe with each other. It's something I had to get a pen and paper out for and draw adjacent ranges to realise that the edge cases boiled down to this comparison.

                如果您想防止任何行重叠,请将此查询的变体放入触发器中:

                If you want to prevent any rows from overlapping, put a variant of this query in a trigger:

                create trigger mytable_no_overlap
                before insert on mytable
                for each row
                begin
                  if exists (select * from mytable
                             where starttime <= new.endtime
                             and endtime >= new.starttime) then
                    signal sqlstate '45000' SET MESSAGE_TEXT = 'Overlaps with existing data';
                  end if;
                end;
                

                这篇关于检查表的时间重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:MySQL 从一个数据库插入另一个数据库 下一篇:mysql 是否具有相当于 Oracle 的“分析函数"?

                相关文章

                <legend id='ivj1t'><style id='ivj1t'><dir id='ivj1t'><q id='ivj1t'></q></dir></style></legend>
                  <i id='ivj1t'><tr id='ivj1t'><dt id='ivj1t'><q id='ivj1t'><span id='ivj1t'><b id='ivj1t'><form id='ivj1t'><ins id='ivj1t'></ins><ul id='ivj1t'></ul><sub id='ivj1t'></sub></form><legend id='ivj1t'></legend><bdo id='ivj1t'><pre id='ivj1t'><center id='ivj1t'></center></pre></bdo></b><th id='ivj1t'></th></span></q></dt></tr></i><div id='ivj1t'><tfoot id='ivj1t'></tfoot><dl id='ivj1t'><fieldset id='ivj1t'></fieldset></dl></div>

                    <tfoot id='ivj1t'></tfoot>

                    <small id='ivj1t'></small><noframes id='ivj1t'>

                      <bdo id='ivj1t'></bdo><ul id='ivj1t'></ul>