Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6087841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:54:58+00:00 2026-05-23T11:54:58+00:00

I have a MySQL table with the following fields: name starttime endtime starttime and

  • 0

I have a MySQL table with the following fields:

  • name
  • starttime
  • endtime

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:54:59+00:00Added an answer on May 23, 2026 at 11:54 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mysql table with following fields id (which auto increments) title,name,age,institution,iod the fields
I have the following mapping for my table in MySql: <class name=Tag, namespace table=tags
I have a simple table in mysql with the following fields: id -- Primary
Let's assume I have 3 tables in a MySQL table with the following fields.
I have the following MySQL table structure: num field company phone website 1 Gas
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have the following table: mysql> SELECT * FROM `bright_promotion_earnings`; +----+----------+------------+----------+-------+ | id |
I have the following table in MySQL (version 5): id int(10) UNSIGNED No auto_increment
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.