Im developing a php booking system based on timeslot for daily basis.
Ive set up 4 database tables!
Bookslot (which store all the ids - id_bookslot, id_user, id_timeslot)
Timeslot (store all the times on 15 minutes gap ex: 09:00, 09:15, 09:30, etc)
Therapist (store all therapist details)
User (store all the members detail)
ID_BOOKSLOT ID_USER ID_THERAPIST ID_TIMESLOT
1 10 1 1 (09:00)
2 11 2 1 (09:00)
3 12 3 2 (09:15)
4 15 3 1 (09:00)
Now, my issue is, it keep showing repeation for timeslot when i want echoing the data for example:
thera a thera b thera c
-------------------------------------------------
09:00 BOOKED available available
09:00 available BOOKED available
09:00 available available BOOKED
09:15 available BOOKED available
as you can see, 09:00 showing three times, and i want something like below
thera a thera b thera c
-------------------------------------------------
09:00 BOOKED BOOKED BOOKED
09:15 available BOOKED available
There might be something wrong with joining the table or else.
The code to join the table
$mysqli->query("SELECT * FROM bookslot RIGHT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot LEFT JOIN therapist ON bookslot.id_therapist = therapist.id_therapist"
if anyone have the solution for this system, please help me out and i appriciate it much!
select
id_TimeSlot
, coalesce(Thera_A, 'available') as Thera_A
, coalesce(Thera_B, 'available') as Thera_B
, coalesce(Thera_C, 'available') as Thera_C
from
(
select
t.id_TimeSlot
, max(case b.id_Therapist when 1 then 'booked' else null end) as Thera_A
, max(case b.id_Therapist when 2 then 'booked' else null end) as Thera_B
, max(case b.id_Therapist when 3 then 'booked' else null end) as Thera_C
from TimeSlot as t
left join BookSlot as b on b.id_TimeSlot = t.id_TimeSlot
left join Therapist as p on p.id_Therapist = b.id_Therapist
group by t.id_TimeSlot
) as xx ;
Test:
create table TimeSLot (id_TimeSLot integer);
create table Therapist (id_Therapist integer);
create table BookSlot (id_Therapist integer, id_TimeSlot integer);
insert into Therapist (id_Therapist)
values (1), (2), (3);
insert into TimeSlot (id_TimeSlot)
values (1), (2), (3), (4), (5);
insert into BookSlot (id_Therapist,id_TimeSlot)
values (1,1), (1,5), (2,1), (2,4), (3,1);
returns
id_TimeSlot Thera_A Thera_B Thera_C
----------------------------------------------
1 booked booked booked
2 available available available
3 available available available
4 available booked available
5 booked available available
I guess you need to GROUP BY id_timeslot, and then check which therapists are booked (or not).
To avoid complicated queries, make table "appointments" (id, u_id, t_id, start, stop, day)...
You can then print appointments on particular day or timespan using BETWEEN start / stop and WHERE day = someday...
Related
I have two tables in mysql (shop_details,booking)
shop_details
id shop_id open_time close_time
1 1 09:00Am 09:00Pm
2 5 10:00Am 09:00Pm
booking
id shop_id start_time end_time
1 1 10:30am 11:10Am
1 5 02:00pm 02:45pm
Now I want if I want to know about shop_id (1) then I want to get booked time (after shop open time) and avaliable time (till shop close) with every half hour, for example I want following result
shop_id start_time end_time status
1 09:00am 09:30am avaliable
1 09:30am 10:00am avaliable
1 10:00am 10:30am avaliable
1 10:30am 11:10am booked
1 11:10am 11:40am avaliable
...
1 08:30am 09:00pm booked
You need a list of times -- then you can get the information using a join or correlated subquery:
select t.tme,
(case when exists (select 1
from booking b
where b.start_time <= t.tme and
b.end_time > t.tme
)
then 'booked' else 'available'
end) as status
from (select cast('00:00' as time) as tme union all
select cast('00:30' as time) as t union all
select cast('01:00' as time) as t union all
. . .
select cast('23:50' as time) as t
) t join
(select sd.*
from shop_details sd
where sd.shop_id = 1
) sd1
on t.tme >= sd1.open_time and
t.tme <= sd1.close_time;
first, use while function to create a loop that will repeat the code until reaching close time,
inside while function, u can use IF statement to make Availability status base on dates
goold luck
I have an issue. I am joining two table and can not get the proper data using MySQL.I am explaining my table below.
db_day:
day_id day_name
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
db_images:
id name from_day to_day
1 Raj 1 3
2 Rahul 4 7
I am explaining my query below.
select sl.id,sl.name,sl.from_day,sl.to_day,d.day_name,d.day_id from db_images as sl left join db_day as d on d.day_id=sl.from_day sl.id desc
Here i need both from day and to day with the name after joining the table but here i am getting the from day only .Please help me to resolve this issue.
Join the db_day table twice with different alias names
select sl.id, sl.name, sl.from_day, sl.to_day,
d1.day_name as from_day, d1.day_id as from_id,
d2.day_name as to_day, d2.day_id as to_id
from db_images sl
left join db_day d1 on d1.day_id = sl.from_day
left join db_day d2 on d2.day_id = sl.to_day
Well, I have 2 mysql tables :
1) clients
client_id conn_date monthly_bill
=========================================
10 2016-06-01 700.00
11 2016-08-30 650.00
12 2016-08-30 1000.00
13 2016-07-01 700.00
2) clients_pay_bill
cbp_id client_id bill_month
===============================
1 10 2016-08-30
2 11 2016-08-12
3 12 2016-08-08
In clients table conn_date column is the date when user first register or connection date.
In clients_pay_bill table bill_month column shows that which month client pay his bill.
Now in a current month I want to show the total due bill from connection date to current date.
For ex : In clients_pay_bill table clients_id = 10 is only paid this 2016-08-30 month bill. But didn't paid this 2016-06-08 and 2016-07-08 months bill. So his due is now = 1400 because his monthly bill is 700.
I can't imagine how the mysql query should look like ! How can I do this ?
Update :
select C.client_id,C.monthly_bill,
(
PERIOD_DIFF(EXTRACT(YEAR_MONTH from CURDATE()),
EXTRACT(YEAR_MONTH from C.conn_date))+1
-
count(P.bill_month)
)*C.monthly_bill as Need_many
from clients C
left join clients_pay_bill P
on P.client_id=C.client_id and P.bill_month<date_format(curdate(),'%Y-%m-01')
group by C.client_id
Function PERIOD_DIFF() return the number of months between periods (current month and connected date). Count() rows in clients_pay_bill return count of payd month. The difference between these values is the count of unpaid months.
Need to insert into the table public_holidays, by the given date that matches holidays table date and it should match CLOCK table clock_in and clock_out time fields and fetch time difference in seconds for 28800 = 8hrs and also need to match designation table from users table user_id, sorry for a very messy code, greatly confused any help could be very helpful thank you.
Clock Table
id user_id date clock_in clock_out created_at updated_at
6 12 2016-06-10 2016-06-10 06:00:00 2016-06-10 14:00:00 2016-06-10 19:20:41 2016-06-10 00:15:51
Users
id first_name last_name designation_id email
1 crysis name 1 crysis#gmail.com
holidays
id date holiday_description created_at updated_at
1 2016-06-10 christmas 2016-06-11 15:23:54 2016-06-11 15:23:54
Designation
designation_id department_id designation created_at updated_at
1 1 employee 2016-01-30 21:03:24 2016-01-30 22:03:24
Department
department_id department_name department_description created_at updated_at
1 IT Info tech 2016-01-30 21:03:24 2016-01-30 21:03:24
Need to insert into this table Public_holidays in the below format by calculating time 8hrs from clock table
user_id department_id designation_id date_cur clock_in clock_out created_at updated_at
12 1 1 2016-06-13 2016-06-10 06:00:00 2016-06-10 14:00:00 2016-06-13 21:03:24 2016-06-13 21:03:24
Tried Messy code
INSERT INTO public_holidays (user_id, department_id,designation_id,clock_in,clock_out) SELECT(cl.user_id,
di.department_id,
di.designation_id,
cl.clock_in,
cl.clock_out)
FROM clock cl, designations di
where
(
select h1.date AS ph_date from holidays h1 WHERE ph_date = 2015-01-22
AND
select (TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out)=28800) AS differ1
AND
INNER JOIN users AS ui ON ui.designation_id = di.id
);
I still don't get the idea, but at least see some relations. Query below will return dataset for all emplyees and all holidays for each of them (h.date column should go into cur_date table). The last JOIN has no relation thus it creates cartesian product - attaches each holiday for each already constructed row (total rows = nuber of empl x number of holidays)
SELECT cl.user_id, di.department_id, di.designation_id, h.date, cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN users u ON cl.user_id = u.user_id
INNER JOIN designation di ON u.designation_id = u.user_id
CROSS JOIN holidays h --cartesian product: all dates for each row
Don't know what clock table is - standard working hours (one row per employee - I assumed that this is true), daily timesheet (one row per date per employee) or sth else? If its standard working hours then why datetime and not time? If its daily timesheet how should I decide which 8 hrs from that table should you choose (and why 8 hrs)?
Dont know why you calculated/searched for these 8 hrs - do you want to insert holidays for empl. that work 8 hrs only (according to "clock"/in any day)
Don't know what cur_date is (assumed the holiday), but why use date next to datetime - shouldn't be either time or date or datetime only?
Finally this one works #shudder thanks for the help
INSERT INTO public_holidays (user_id, department_id,designation_id,date_cur,clock_in,clock_out)
SELECT cl.user_id, des.department_id , us.designation_id, cl.date,cl.clock_in, cl.clock_out
FROM clock cl
INNER JOIN holidays AS hol ON hol.date = cl.date
INNER JOIN users AS us ON cl.user_id = us.id
INNER JOIN designations AS des ON des.id = us.designation_id
WHERE date(cl.created_at) = '2016-06-13'
AND TIMESTAMPDIFF(second,cl.clock_in, cl.clock_out) = 28800;
I have 2 tables,
prices (pcode, date, priceperweek)
1 record covers 1 week
booked (pcode, date)
1 record = 1 day, because shortbreaks are available
And a form consists of 3 fields
startdate
flexibility (+/- 1/2/3 weeks)
duration (3 / 4 / 5 / 6 / 7 / 14)).
The below query should be selecting prices & dates and checking to see if the selected start date appears in the prices table and that each day from the startdate doesn't appear in the bookings table.
SELECT SUM(priceperweek) AS `ppw`, prices.date AS `startdate`
FROM `prices` LEFT JOIN `booked` ON prices.pcode=booked.pcode
WHERE prices.pcode='A2CD59GH'
AND (prices.date IN ('20131221', '20131228')
AND booked.date NOT IN ('20131221', '20131222', '20131223',
'20131224', '20131225', '20131226', '20131227', '20131228',
'20131229', '20131230', '20131231', '20140101', '20140102',
'20140103')
)
OR (prices.date IN ('20131214', '20131221')
AND booked.date NOT IN ('20131214', '20131215', '20131216',
'20131217',
'20131218', '20131219', '20131220', '20131221', '20131222',
'20131223', '20131224', '20131225', '20131226', '20131227')
)
OR (prices.date IN ('20131228', '20140104') AND booked.date NOT IN
('20131228', '20131229', '20131230', '20131231', '20140101',
'20140102', '20140103', '20140104', '20140105', '20140106',
'20140107', '20140108', '20140109', '20140110')
)
GROUP BY prices.date
ORDER BY prices.date ASC
VALUES GIVEN TO QUERY...
startdate = 20131221
duration = 14
property = A2CD59GH
plusminus = 1
My problem is that this query returns records even if some of the dates in a range appear in the "bookings" table AND the ppw value is alot more than i would have expected.
The reason for using SUM(ppw) is when a duration of 14 is specified the price will sum both weeks together.
Thanks for any help on this
The problem with your approach is that the startdate will only be filtered from the results if every single record in the booked table for the given pcode falls within the booking period. Obviously this won't be the case if the property has been booked on some other date.
I'd suggest performing an anti-join along the following lines:
SELECT t.date, SUM(prices.priceperweek) FROM prices JOIN (
SELECT prices.date
FROM prices LEFT JOIN booked
ON booked.pcode = prices.pcode
AND booked.date BETWEEN prices.date
AND prices.date + INTERVAL 14 DAY
WHERE booked.pcode IS NULL
AND prices.pcode = 'A2CD59GH'
AND prices.date BETWEEN '20131221' - INTERVAL 1 WEEK
AND '20131221' + INTERVAL 2 WEEK
) t ON prices.date BETWEEN t.date AND t.date + INTERVAL 13 DAY
GROUP BY t.date