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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:57+00:00 2026-06-13T22:11:57+00:00

I’m working on a reservation system in MySQL and I’m having a bit of

  • 0

I’m working on a reservation system in MySQL and I’m having a bit of trouble with my query for finding the next available appointment slots.

My days are broken down into 15-minute slots (00:00:00 – 00:14:59, 00:15:00 – 00:29:59, etc.). Table structure so far:

appointments table:

CREATE TABLE IF NOT EXISTS `appointments` (
`appointmentID` int(9) unsigned NOT NULL AUTO_INCREMENT,
`patientID` int(9) unsigned NOT NULL,
`apptTitle` varchar(50) NOT NULL,
`apptDT` datetime NOT NULL,
`apptLength` int(4) NOT NULL,
`apptStatus` varchar(25) NOT NULL,
`physician` int(9) unsigned NOT NULL,
`apptType` varchar(30) NOT NULL,
`apptLocation` varchar(25) NOT NULL,
PRIMARY KEY (`appointmentID`),
KEY `patientID` (`patientID`),
KEY `physician` (`physician`),
KEY `apptStatus` (`apptStatus`),
KEY `apptLocation` (`apptLocation`),
KEY `apptType` (`apptType`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

apptSlots Table:

CREATE TABLE IF NOT EXISTS `apptSlots` (
`startDT` datetime NOT NULL,
`endDT` datetime NOT NULL,
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

The apptSlots table is already filled with all the 15 minute slots for 2012, as I explained above.

I have a query that can successfully show me the slots that aren’t taken in any way (no appointments scheduled at all).

SELECT a.*
FROM apptSlots AS a
LEFT JOIN appointments AS b
ON a.endDT
BETWEEN b.apptDT AND DATE_ADD(b.apptDT, INTERVAL b.apptLength MINUTE)
AND b.appointmentID IS NULL

What I need to do now is search for appointment slots across multiple physicians, multiple rooms, etc. For example, if Physician A makes an appointment for 7am – 8am, that time slot no longer shows up in my query above. But Physician B and C don’t have any appointments for that slot, so I need to be able to schedule them. The same goes for rooms, Room A is used by the 7am appointment, but room B and C are still free to be scheduled.

How can I determine open slots with all these variables? I know I can test with WHERE conditions for each combination and this works if I need to test for a specific doctor, or a specific room for a treatment. But, if I just want to see all slots across all doctors and rooms, my query doesn’t work and testing all possible combinations individually doesn’t seem right, it’s just a matter of how to get the right conditions into the LEFT JOIN clause.

Please let me know if I’m being unclear, but I think this is enough info. If not, I’ll add in whatever you ask for!

Thank you for your help, my previous question on here got me as far as my query above, now I just need some refinement. Ask any questions you need, I’ll be here to answer them!

  • 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-06-13T22:11:58+00:00Added an answer on June 13, 2026 at 10:11 pm

    As requested, here’s an answer put together from my various comments:

    Note that you can’t have just one table for appointment slots. Each doctor will have a different schedule, including vacations, etc. The doctor_id should be part of an available_appointments table.

    You also have “resources”, like the appointment room, or portable equipment, etc. These should be in a different table (available_resources?), with their own availability and scheduling.

    As for finding blocks of time, try this:

    set @last_time = null;
    set @block_size = 1;
    
    select *
    from (
      select startDT,
        case when @last_time is null or startDT = date_add(@last_time, interval 15 minute)
          then @block_size := @block_size + 1
          else @block_size := 1
        end as block_size,
        @last_time := startDT
      from foo_appt
    ) foo
    where block_size = 4;
    

    EDIT:

    Here was my test data:

    +---------------------+
    | startDT             |
    +---------------------+
    | 2012-11-06 13:00:00 |
    | 2012-11-06 13:15:00 |
    | 2012-11-06 14:00:00 |
    | 2012-11-06 14:15:00 |
    | 2012-11-06 14:30:00 |
    | 2012-11-06 14:45:00 |
    | 2012-11-06 16:00:00 |
    | 2012-11-06 16:15:00 |
    +---------------------+
    

    So there’s a 30-minute block, then an hour block, then another 30-minute block.

    The inner query does the heavy lifting and produces the following output. The last two columns are just to maintain the two variables, and should be ignored.

    +---------------------+------------+-------------+-----------------------+
    | startDT             | block_size | @block_size | @last_time := startDT |
    +---------------------+------------+-------------+-----------------------+
    | 2012-11-06 13:00:00 |          1 |           1 | 2012-11-06 13:00:00   |
    | 2012-11-06 13:15:00 |          2 |           2 | 2012-11-06 13:15:00   |
    | 2012-11-06 14:00:00 |          1 |           1 | 2012-11-06 14:00:00   |
    | 2012-11-06 14:15:00 |          2 |           2 | 2012-11-06 14:15:00   |
    | 2012-11-06 14:30:00 |          3 |           3 | 2012-11-06 14:30:00   |
    | 2012-11-06 14:45:00 |          4 |           4 | 2012-11-06 14:45:00   |
    | 2012-11-06 16:00:00 |          1 |           1 | 2012-11-06 16:00:00   |
    | 2012-11-06 16:15:00 |          2 |           2 | 2012-11-06 16:15:00   |
    +---------------------+------------+-------------+-----------------------+
    

    The outer query allows you to get just the ones with the appropriate block_size. You’re getting the start time of the 4th block.

    MySQL doesn’t let you set multiple variables at once in a query (you have to do that in a procedure), which is disappointing when trying to mess around like this.

    The easiest thing that comes to mind would be to take the 4th period and subtract back to the start of the first period, so the outer query would look like this:

    select date_sub(startDT, interval 15 * (block_size-1) minute) as appointment_time,
        block_size*15 as appointment_duration
    from ( ...
    
    +---------------------+----------------------+
    | appointment_time    | appointment_duration |
    +---------------------+----------------------+
    | 2012-11-06 14:00:00 |                   60 |
    +---------------------+----------------------+
    

    Hope that helps.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.