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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:05:20+00:00 2026-05-11T01:05:20+00:00

Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL

  • 0

Hey, I stumbled upon this site looking for solutions for event overlaps in mySQL tables. I was SO impressed with the solution (which is helping already) I thought I’d see if I could get some more help…

Okay, so Joe want’s to swap shifts with someone at work. He has a court date. He goes to the shift swap form and it pull up this week’s schedule (or what’s left of it). This is done with a DB query. No sweat. He picks a shift. From this point, it gets prickly.

So, first, the form passes the shift start and shift end to the script. It runs a query for anyone who has a shift that overlaps this shift. They can’t work two shifts at once, so all user IDs from this query are put on a black list. This query looks like:

SELECT DISTINCT user_id FROM shifts WHERE FROM_UNIXTIME('$swap_shift_start') < shiftend AND FROM_UNIXTIME('$swap_shift_end') > shiftstart 

Next, we run a query for all shifts that are a) the same length (company policy), and b) don’t overlap with any other shifts Joe is working.

What I currently have is something like this:

SELECT * FROM shifts AND shiftstart BETWEEN  FROM_UNIXTIME('$startday') AND FROM_UNIXTIME('$endday') AND user_id NOT IN ($busy_users)  AND (TIME_TO_SEC(TIMEDIFF(shiftend,shiftstart)) = '$swap_shift_length') $conflict_dates ORDER BY shiftstart, lastname 

Now, you are probably wondering ‘what is $conflict_dates???’

Well, when Joe submits the swap shift, it reloads his shifts for the week in case he decides to check out another shift’s potential. So when it does that first query, while the script is looping through and outputting his choices, it is also building a string that looks kind of like:

AND NOT( 'joe_shift1_start' < shiftend AND 'joe_shift1_end' > shiftstart) AND NOT( 'joe_shift2_start' < shiftend AND 'joe_shift2_end' > shiftstart) ...etc 

So that the database is getting a pretty long query along the lines of:

SELECT * FROM shifts AND shiftstart BETWEEN  FROM_UNIXTIME('$startday') AND FROM_UNIXTIME('$endday') AND user_id NOT IN ('blacklisteduser1', 'blacklisteduser2',...etc)  AND (TIME_TO_SEC(TIMEDIFF(shiftend,shiftstart)) = '$swap_shift_length') AND NOT( 'joe_shift1_start' < shiftend AND 'joe_shift1_end' > shiftstart) AND NOT( 'joe_shift2_start' < shiftend AND 'joe_shift2_end' > shiftstart) AND NOT( 'joe_shift3_start' < shiftend AND 'joe_shift3_end' > shiftstart) AND NOT( 'joe_shift4_start' < shiftend AND 'joe_shift4_end' > shiftstart) ...etc ORDER BY shiftstart, lastname 

So, my hope is that either SQL has some genius way of dealing with this in a simpler way, or that someone can point out a fantastic logical principal that accounts for the potential conflicts in a much smarter way. (Notice the use of the ‘start > end, end < start’, before I found that I was using betweens and had to subtract a minute off both ends.)

Thanks!

A

  • 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. 2026-05-11T01:05:20+00:00Added an answer on May 11, 2026 at 1:05 am

    I think you should be able to exclude Joe’s other shifts using an inner select instead of the generated string, something like:

    SELECT * FROM shifts s1 AND shiftstart BETWEEN  FROM_UNIXTIME('$startday') AND FROM_UNIXTIME('$endday') AND user_id NOT IN ($busy_users)  AND (TIME_TO_SEC(TIMEDIFF(shiftend,shiftstart)) = '$swap_shift_length') AND (SELECT COUNT(1) FROM shifts s2      WHERE s2.user_id = $joes_user_id      AND   s1.shiftstart < s2.shiftend      AND   s2.shiftstart < s1.shiftend) = 0 ORDER BY shiftstart, lastname 

    Basically, each row has an inner query for the count of Joe’s shifts which overlap, and makes sure that it’s zero. Thus, only rows which don’t overlap with any of Joe’s existing shifts will be returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, my Computational Science course this semester is entirely in Java. I was
I recenty stumbled upon some files described as HTML Applications on my Win XP
Hey all, I am not quite getting why this doesn't seem to be working.
Hey guys, I have a client who wants their front site to have a
hey guys, i'm almost sure that i've stumbled across a plugin like that. i
Hey, I'm trying to make an Address book for my site and I was
Hey, Sorry if this is a dumb question. I'm an amateur programmer and new
Hey guys, I just started learning C++ and I have this code that I
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first

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.