Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

How to find open appointment slots with MySQL query based on current table structure

I have three tables, one called

user_hours (where users hours are stored) - every entry represents the hours for one day only, for that user:
User generated image
user_hours_breaks (where breaks which occur during user_hours are stored -  every entry represents the break hours, if any, for one day only, for that user):
User generated image
appointments (where all appointment entries are stored):
User generated image
How do I write a MySQL query to find open time slots with 2 hour windows based upon a users hours (taking into account breaks)

Thanks..
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Images are often excellent ways to help communicate, but writing DDL from images is tedious, it would be so much more helpful if you provided that so we volunteers don't have to (& obviously you already have that DDL)

Similarly it is always helpful to supply SAMPLE DATA for each table
(also in a form that we can use and not have to type)

Then, so that everyone knows precisely what you are seeking, you should provide the EXPECTED RESULT
(derived only from the sample)

-------------------
ps: It's not a matter of being lazy, it's just that we try to answer so many questions.
making it easier to answer helps you too of course (get solution faster)
Avatar of Mark
Mark

ASKER

No problem, will do so in the morning

Thanks
Avatar of Mark

ASKER

3 tables:
------------------------
user_hours (where users hours are stored) - every entry represents the hours for one day only, for that user:

columns:
=========
id
user
start_time
end_time

=========
rows:
=========

"1","52","2015-03-24 09:00:00","2015-03-24 19:00:00"
"1","16","2015-03-24 09:00:00","2015-03-24 19:00:00"

------------------------

user_hours_breaks (where breaks which occur during user_hours are stored -  every entry represents the break hours, if any, for one day only, for that user):

columns:
=========
id
user
start_time
end_time

=========
rows:
=========

"4","52","2015-03-24 16:00:00","2015-03-24 16:30:00"

------------------------

appointments (where all appointment entries are stored):

columns:
=========
id
user
start_time
end_time

=========
rows:
=========
"1","52","8553","2015-03-24 13:00:00","2015-03-24 15:00:00"

------------------------

Expected outcome...

function get_appointment_times_starting_now($startFromMinute='00',$appointmentDurationInMinutes,$ShowDays=1) {

      .......
      
}

so executing get_appointment_times_starting_now('00', 60,1) at 10:36AM should return:

Appointment Available With User ID 52 On 2015-03-24 From 11AM to 12PM
Appointment Available With User ID 16 On 2015-03-24 From 11AM to 12PM
Appointment Available With User ID 52 On 2015-03-24 From 12PM to 1PM
Appointment Available With User ID 16 On 2015-03-24 From 12PM to 1PM
Appointment Available With User ID 16 On 2015-03-24 From 1PM to 2PM
Appointment Available With User ID 52 On 2015-03-24 From 3PM to 4PM
Appointment Available With User ID 16 On 2015-03-24 From 2PM to 3PM
Appointment Available With User ID 16 On 2015-03-24 From 3PM to 4PM
Appointment Available With User ID 16 On 2015-03-24 From 4PM to 5PM
Appointment Available With User ID 52 On 2015-03-24 From 5PM to 6PM
Appointment Available With User ID 16 On 2015-03-24 From 5PM to 6PM
Appointment Available With User ID 52 On 2015-03-24 From 6PM to 7PM
Appointment Available With User ID 16 On 2015-03-24 From 6PM to 7PM

executing get_appointment_times_starting_now('00', 60,2) at 10:36AM should return 2 days worth of available appointments

executing get_appointment_times_starting_now('30', 60,2) at 10:36AM should return 2 days worth of available appointments but with starting with the nearest half hour, example:

Appointment Available With User ID 52 On 2015-03-24 From 11:30AM to 12:30PM

executing get_appointment_times_starting_now('00', 120,1) at 10:36AM should return appointments that can fit 2 hour intervals
Here's what  I used from the above to suit the http://SQLfiddle.com "Text to DDL" button.
Note there is an unknown column in table appointments.

user_hours
id, user, start_time, end_time
1,52,2015-03-24 09:00:00,2015-03-24 19:00:00
1,16,2015-03-24 09:00:00,2015-03-24 19:00:00

user_hours_breaks
id, user, start_time, end_time
4,52,2015-03-24 16:00:00,2015-03-24 16:30:00

appointments
id,user,unknown,start_time,end_time
1,52,8553,2015-03-24 13:00:00,2015-03-24 15:00:00

It generated this (the DDL):
CREATE TABLE user_hours
    (`id` int, `user` int, `start_time` datetime, `end_time` datetime)
;
    
INSERT INTO user_hours
    (`id`, `user`, `start_time`, `end_time`)
VALUES
    (1, 52, '2015-03-24 09:00:00', '2015-03-24 19:00:00'),
    (1, 16, '2015-03-24 09:00:00', '2015-03-24 19:00:00')
;


CREATE TABLE user_hours_breaks
    (`id` int, `user` int, `start_time` datetime, `end_time` datetime)
;
    
INSERT INTO user_hours_breaks
    (`id`, `user`, `start_time`, `end_time`)
VALUES
    (4, 52, '2015-03-24 16:00:00', '2015-03-24 16:30:00')
;




CREATE TABLE appointments
    (`id` int, `user` int, `unknown` int, `start_time` datetime, `end_time` datetime)
;
    
INSERT INTO appointments
    (`id`, `user`, `unknown`, `start_time`, `end_time`)
VALUES
    (1, 52, 8553, '2015-03-24 13:00:00', '2015-03-24 15:00:00')
;

Open in new window

Avatar of Mark

ASKER

Sorry, didn't understand the term DDL before but that clears it up
Avatar of Mark

ASKER

The unknown column in appointments you are referring to is not necesarry for this question as it is just a reference to the client ID which the appointment is for.
DDL = "data definition language" it is a subset of SQL

I have had some just had some bad news and may not be available for a short while.
I hope this won't be the case, but it could be.
Avatar of Mark

ASKER

No problem, will be out until Thursday morning before I can test..

Thanks
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

Thanks! I will test it when I get back Thursday morning.
Avatar of Mark

ASKER

Works PERFECT! Thanks ... could you also help in this question (it works fine through PHPMyAdmin but I have an issue running it through MySQLi)

https://www.experts-exchange.com/questions/28644685/Why-can't-I-run-this-query-through-PHP-mysql-query-but-works-fine-directly-using-PHPMyAdmin.html?anchorAnswerId=40690436#a40690436