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 800303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:16:21+00:00 2026-05-14T23:16:21+00:00

I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),

  • 0

I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar), …

I need to find time slots that are occupied by two or more rows. Here is an example table

id|        date         |starttime|endtime |
__|_____________________|_________|________|
1 | 2010-02-16 17:37:36 |14:35:00 |17:37:00|
2 | 2010-02-17 12:24:22 |12:13:00 |14:32:00|
3 | 2010-02-16 12:24:22 |15:00:00 |18:00:00|

Rows 1 and 3 collide, and need to be corrected by the user. I need a query to identify such colliding rows – something that would give me the ID of all rows in the collision.

When inserting data in the database I find collisions with this query:

SELECT ID FROM LEDGER 
WHERE
    DATE(DATE) = DATE('$timestamp')  
    AND (
        STR_TO_DATE('$starttime','%H:%i:%s') BETWEEN 
            STR_TO_DATE(STARTTIME,'%H:%i:%s') AND STR_TO_DATE(ENDTIME,'%H:%i:%s') OR
        STR_TO_DATE('$endtime','%H:%i:%s') BETWEEN 
            STR_TO_DATE(STARTTIME,'%H:%i:%s') AND STR_TO_DATE(ENDTIME,'%H:%i:%s') 
     ) AND
     FNAME = '$fname'";

Is there any way to accomplish this strictly using MySQL or do I have to use PHP to find the collisions?

Edit: Quassnoi’s sollution helped me create the exact query I was needing. This is it:

SELECT  l1.id as id1, l2.id as id2, l1.lname as name1, l2.lname as name2, 
   l1.date as date1, l2.date as name2, l1.starttime as starttime1, 
   l2.starttime as starttime2, l1.endtime as endtime1, l2.endtime as endtime2
FROM    ledger l1
JOIN    ledger l2
ON      l2.starttime <= l1.endtime
    AND l2.endtime >= l1.starttime
    AND l2.lname = l1.lname
    AND l2.id != l1.id
    AND DATE(l2.date)=DATE(l1.date)
  • 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-14T23:16:21+00:00Added an answer on May 14, 2026 at 11:16 pm

    You could issue this query:

    SELECT  l1.id, l2.id
    FROM    ledger l1
    JOIN    ledger l2
    ON      ADDTIME(CAST(CAST(l2.date AS DATE) AS DATETIME), l2.starttime) <= ADDTIME(CAST(CAST(l1.date AS DATE) AS DATETIME), l1.endtime)
            AND ADDTIME(CAST(CAST(l2.date AS DATE) AS DATETIME), l2.endtime) <= ADDTIME(CAST(CAST(l1.date AS DATE) AS DATETIME), l1.starttime)
            AND l1.id < l2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following MySQL (version 5.1) table (InnoDB): Username varchar(50) Sequence int(11) FileType
I have a database table with the following columns: idCompany:INT, Data:LONGBLOB, Extension:VARCHAR(5), Name:VARCHAR(45). I
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
I have a (mysql) database table with the following columns: NAME | String (Unique)
I have created a mysql table with the following columns... item_price, discount, delivery_charge, grand_total
So I have an example table called items with the following columns: item_id (int)
I have a MySQL table with (among others) the following columns: [name] [member_since_date] [member_until_date]
I have 2 mysql tables : Question with the following columns : id, question,
I have a MSSQL table stores that has the following columns in a table:
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,

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.