Mysql Insert query with multiple where and join operations - php

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;

Related

PHP/SQL - How to count saturdays in each month?

I have two MySQL tables, called "accounts" and "events".
Accounts
ID
name
1
Pete
2
Josh
3
Harry
Events
ID
date
account_id
1
2021-10-09
1
2
2021-09-25
1
3
2021-10-23
2
4
2021-11-06
1
5
2021-10-13
1
6
2021-11-17
2
7
2021-11-06
3
8
2021-12-04
3
The account_id in the events table is linked to the id in the accounts table.
My question is: which query can I use to count saturdays in each month (date YYYY-mm-dd format) for each unique user in the accounts table? So I get the next result:
Name
September
October
November
December
Josh
0
1
0
0
Pete
1
1
1
0
Harry
0
0
1
1
I've tried many queries (with i.e. the (inner) JOIN, DISTINCT and GROUP BY keywords) but I don't get the exact result. Can you please help me?
Many thanks in advance!
Basically you can use DAYOFWEEK function and GROUP BY MONTH
SELECT
account_id,
MONTH(date),
COUNT(*)
FROM Events
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date);
SQL Fiddle here
and when you can use PIVOT on received table like:
WITH res AS (
SELECT
account_id,
MONTH(date) mnth,
COUNT(*) cnt
FROM Events
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date)
) SELECT
account_id,
name,
SUM(mnth=1) Januar,
--
SUM(mnth=9) September,
SUM(mnth=10) October,
SUM(mnth=11) November,
SUM(mnth=12) December
FROM res
JOIN Accounts ON Accounts.id = account_id
GROUP BY account_id, name;
SQL Pivot Fiddle

Can not get two day name after joining two table using MySQL

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

MYSQL select query with multiple tables

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

Update statement with join and two where conditions

This is what I need the update to do:
update my summary table TIME column with the user-edited TIME value
compare the new TIME, using ID's from my summary table and hour_interval table to join tables
find the correct row ID where the new TIME falls within (there are two columns with start and end hours; example would be: start column - 04:00:00 end column 06:00:00)
update the summary table with the correct interval row ID using the summary table ID
The code executes, but doesn't update the interval ID. Suggestions?
UPDATE summary S
JOIN hour_interval H
ON S.hourinterval_id = H.hourinterval_id
SET S.hourinterval_id = H.hourinterval_id
WHERE ('$new_time' BETWEEN H.start_hour AND H.end_hour)
AND summary_id = '$summary_id'"
hourinterval_id start_hour end_hour
1 4:00:00 5:59:59
2 6:00:00 7:59:59
3 8:00:00 9:59:59
4 10:00:00 11:59:59
5 12:00:00 13:59:59
6 14:00:00 15:59:59
7 16:00:00 17:59:59
8 18:00:00 19:59:59
Try this:
UPDATE summary S
JOIN hour_interval H
ON '$new_time' BETWEEN H.start_hour AND H.end_hour
SET S.hourinterval_id = H.hourinterval_id
WHERE summary_id = '$summary_id'"

PHP Booking timeslot

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...

Categories