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

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

        <legend id='sC3r2'><style id='sC3r2'><dir id='sC3r2'><q id='sC3r2'></q></dir></style></legend>

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

        <i id='sC3r2'><tr id='sC3r2'><dt id='sC3r2'><q id='sC3r2'><span id='sC3r2'><b id='sC3r2'><form id='sC3r2'><ins id='sC3r2'></ins><ul id='sC3r2'></ul><sub id='sC3r2'></sub></form><legend id='sC3r2'></legend><bdo id='sC3r2'><pre id='sC3r2'><center id='sC3r2'></center></pre></bdo></b><th id='sC3r2'></th></span></q></dt></tr></i><div id='sC3r2'><tfoot id='sC3r2'></tfoot><dl id='sC3r2'><fieldset id='sC3r2'></fieldset></dl></div>
      2. 检查 MySQL 中日期范围的重叠

        时间:2023-08-18
        <tfoot id='iNIJp'></tfoot>

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

              • <small id='iNIJp'></small><noframes id='iNIJp'>

                <legend id='iNIJp'><style id='iNIJp'><dir id='iNIJp'><q id='iNIJp'></q></dir></style></legend>
                  <tbody id='iNIJp'></tbody>

                  <i id='iNIJp'><tr id='iNIJp'><dt id='iNIJp'><q id='iNIJp'><span id='iNIJp'><b id='iNIJp'><form id='iNIJp'><ins id='iNIJp'></ins><ul id='iNIJp'></ul><sub id='iNIJp'></sub></form><legend id='iNIJp'></legend><bdo id='iNIJp'><pre id='iNIJp'><center id='iNIJp'></center></pre></bdo></b><th id='iNIJp'></th></span></q></dt></tr></i><div id='iNIJp'><tfoot id='iNIJp'></tfoot><dl id='iNIJp'><fieldset id='iNIJp'></fieldset></dl></div>
                  本文介绍了检查 MySQL 中日期范围的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  此表用于存储会话(事件):

                  This table is used to store sessions (events):

                  CREATE TABLE session (
                    id int(11) NOT NULL AUTO_INCREMENT
                  , start_date date
                  , end_date date
                  );
                  
                  INSERT INTO session
                    (start_date, end_date)
                  VALUES
                    ("2010-01-01", "2010-01-10")
                  , ("2010-01-20", "2010-01-30")
                  , ("2010-02-01", "2010-02-15")
                  ;
                  

                  我们不想在范围之间发生冲突.
                  假设我们需要插入一个从 2010-01-052010-01-25 的新会话.
                  我们想知道冲突的会话.

                  We don't want to have conflict between ranges.
                  Let's say we need to insert a new session from 2010-01-05 to 2010-01-25.
                  We would like to know the conflicting session(s).

                  这是我的查询:

                  SELECT *
                  FROM session
                  WHERE "2010-01-05" BETWEEN start_date AND end_date
                     OR "2010-01-25" BETWEEN start_date AND end_date
                     OR "2010-01-05" >= start_date AND "2010-01-25" <= end_date
                  ;
                  

                  结果如下:

                  +----+------------+------------+
                  | id | start_date | end_date   |
                  +----+------------+------------+
                  |  1 | 2010-01-01 | 2010-01-10 |
                  |  2 | 2010-01-20 | 2010-01-30 |
                  +----+------------+------------+
                  

                  有没有更好的方法来获得它?

                  Is there a better way to get that?

                  小提琴

                  推荐答案

                  我曾经在一个日历应用程序中遇到过这样的问题.我想我使用了这样的东西:

                  I had such a query with a calendar application I once wrote. I think I used something like this:

                  ... WHERE new_start < existing_end
                        AND new_end   > existing_start;
                  

                  UPDATE 这绝对有效((ns, ne, es, ee) = (new_start, new_end, existing_start, existing_end)):

                  UPDATE This should definitely work ((ns, ne, es, ee) = (new_start, new_end, existing_start, existing_end)):

                  1. ns - ne - es - ee:不重叠且不匹配(因为 ne
                  2. ns - es - ne - ee:重叠和匹配
                  3. es - ns - ee - ne:重叠和匹配
                  4. es - ee - ns - ne:不重叠且不匹配(因为 ns > ee)
                  5. es - ns - ne - ee:重叠和匹配
                  6. ns - es - ee - ne:重叠和匹配

                  <小时>

                  这是一个小提琴

                  这篇关于检查 MySQL 中日期范围的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 MySQL 中获取下一条/上一条记录? 下一篇:如何在 MySQL 上获取两个日期之间相差的天数?

                  相关文章

                  最新文章

                  1. <small id='2rDxP'></small><noframes id='2rDxP'>

                      <bdo id='2rDxP'></bdo><ul id='2rDxP'></ul>
                    <legend id='2rDxP'><style id='2rDxP'><dir id='2rDxP'><q id='2rDxP'></q></dir></style></legend>

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