Update statement with join and two where conditions - php

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'"

Related

Find Records form date field and time field in one day before in mysql

I have the following structure as a table, I want to select the record from one day before, I have tried the following query for selecting the record.
SELECT slot_date, slot_time FROM slot_booking WHERE slot_date =
DATE_SUB(CURDATE(), INTERVAL 24 HOUR)
assume if today is (2018-04-03) time is 09 (24hr Format) I want to select a record from DB 2018-04-04 TIME between 09:00:00 to 9.30:00
i am struggling with compare time field, if you have any suggestion or solution post you answer
Concat date and time fields, use str_to_date to convert to datetime then use date_add or date_sub as required
SET #DT = '2018-04-14';
SET #T = '09:00:00';
set #now = '2018-04-13 09:00:00';
SELECT STR_TO_DATE(CONCAT(#DT,#T),'%Y-%m-%d%H:%i:%s') dt,
case when STR_TO_DATE(CONCAT(#DT,#T),'%Y-%m-%d%H:%i:%s')
between date_add(#now, interval 24 hour) and date_add(date_add(#now, interval 24 hour),interval 30 minute) then 'true'
else 'false'
end as inrange;
+---------------------+---------+
| dt | inrange |
+---------------------+---------+
| 2018-04-14 09:00:00 | true |
+---------------------+---------+
1 row in set (0.00 sec)

Mysql Insert query with multiple where and join operations

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;

How many rows contain a specific value using php and mysqli

I have a table "bookings" that looks like
bookings
id id_class date day name
1 2 2016-03-10 16:00:00 monday Fredrik
2 5 2016-03-11 16:00:00 tuesday Richard
3 7 2016-03-11 18:00:00 tuesday Sara
4 4 2016-03-11 15:00:00 tuesday Fredrik
Then I extract that table using php
<?php
$result = $con->query("select * from bookings");
<?
What I want now is a php function that tells me how many rows that contain a specific entry such as "id_class=2" or how many rows contain (name=Fredrik) where the answer should be between 0 and a positive number. Is there a way for this?
Use where to get specific condition
SELECT COUNT(*) FROM bookings WHERE id_class=2
SELECT COUNT(*) FROM bookings WHERE name='Fredrik'

Difference between two date and print seconds of each day by using mysql and php

I a have table in mysql with some data like this:
id from to
---- -------------------- ---------------------------
1 2013-01-31 23:50:00 2013-02-02 09:00:00
2 2013-02-05 11:21:12 2013-02-08 01:01:01
3 2013-02-08 17:33:44 2013-02-08 18:22:55
4 2013-02-12 01:40:12 2013-02-12 02:00:59
5 2013-02-28 01:40:12 2013-03-02 02:00:59
I need a Mysql query or a php code for finding difference between 'from' column and 'to' column of each rows, and find how many seconds are there in each day between 'from' and 'to' date separately, for example for row 1 the needed output be something like this:
difference between 2013-01-31 23:50:00 - 2013-02-02 09:00:00 for row 1
2013-01-31 : 600 sec (24:00:00 - 23:50:00 => 600 sec)
2013-02-01 : 86400 sec (24:00:00 - 00:00:00 => 86400 sec)
2013-02-02 : 32400 sec (09:00:00 - 00:00:00 => 32400 sec)
and so on ... for each row
MySQL command is preferred. which code can create this output? is there any specific function in php or mysql for creating this output?
I read these answers:
mysql calculate seconds between two date/times for each day
MySQL: how to get the difference between two timestamps in seconds
but these are not my answer.
for information:
link 1 is a question like my question but the answer is not true because "The command needs grouped by day" but how?!
and the second is not my question but question one marked as duplicated with link 2 and it is not true.
Let me assume that you have a table of dates. If so, you can use a join or correlated subquery:
select d.date,
(select sum(timestampdiff(second,
greatest(d.date, t.from),
least(d.date, t.to)
))
from table t
where t.to >= d.date and
t.from < date_add(d.date, interval 1 day)
) as NumSeconds
from dates d;
If you don't have a dates table of some sort, you can create one on the fly:
from (select date('2013-01-31') as date union all
select date('2013-02-01') union all
. . .
) dates

Getting rows of a day by timestamp, Day/Minute of certain pattern

id name quantity timestamp
1 item1 2 2015-06-01 20:00:00
2 item2 5 2015-06-01 22:30:00
3 item3 2 2015-06-02 20:00:00
4 item4 7 2015-06-02 20:30:00
5 item5 9 2015-06-02 21:30:00
This is an example database, 'timestamp' is in datetime format and contains varying values. Note that the table contains various date and time data. How do i get all the rows of one day and of certain minute(above eg: 30) only, eg how can i select all the rows with timestamp 2015-06-01 **:30:00, here ** = hour any help, thanks.
You can use DATE & MINUTE functions -
SELECT * from your_table WHERE DATE(`timestamp`) = '2015-06-02' AND MINUTE(`timestamp`) = 30
You need to write follow query:
SELECT * FROM Your_Table_Name WHERE DATE(timestamp) = '2015-06-01' AND TIME(timestamp) LIKE '%:30:00'

Categories