MySQL TIME() BETWEEN datetime issue - mysql

I'm trying to find to which shift belongs a datetime field.
Shifts are defines as time, and I have a startingHour and endingHour.
The query
SELECT * FROM shifts WHERE TIME('2009-11-20 06:35:00') BETWEEN '06:00:00' and '19:00:00'
works perfect, but when the shift is set to start 19:00:00 to 06:00:00 and the time is 23:35:00 it doesn't return anything
WHERE TIME('2009-11-20 23:35:00') BETWEEN '19:00:00' and '06:00:00'
that line isn't returning anything though I do have records on the table
Thanks
That's the shifts table.
if I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 18:23:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get the proper result, but when I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get no result.

These are two cases: start time < end time and start time > end time. You need something like this:
where (start_time < end_time and $t >= start_time and $t < end_time)
or (start_time >= end_time and ($t < start_time or $t >= end_time))

Since '19:00:00' is greater than '06:00:00' then:
BETWEEN '19:00:00' and '06:00:00'
returns 0 (false) and you get no rows.
One way to get the results that you want is to use CASE like this:
.................
AND 1 = CASE
WHEN a.horaInicio <= a.horaFin THEN TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin
ELSE (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND '23:59:59')
OR (TIME('2019-11-22 02:45:00') BETWEEN '00:00:00' AND a.horaFin)
END

Related

how to get single result in using 2 table data

i have to table 2 in same structure .i already get the result by using one table using following query
SELECT *,COUNT(Time),
CASE WHEN COUNT(Time) = 1
THEN CASE WHEN Time > '00:00:00' && Time <= '12:15:00'
THEN Time END ELSE MIN(Time)
END as time_in,
CASE WHEN COUNT(Time) = 1
THEN CASE WHEN Time > '12:15:00' && Time <= '23:59:59'
THEN Time END ELSE NULLIF(MAX(Time), MIN(Time))
END as time_out
FROM attendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%'
GROUP BY EnrolledID,Date
this query will time devide in to the 2 part(time in and time out) .it work fine.now want to get the data from anther table also.it also have same structure attendancedata table.
attendancedata table structure
EnrolledID Date Time
23 2019-09-09 16:19:00
53 2019-08-27 08:19:00
tempattendancedata table structure
EnrolledID Date Time
23 2019-09-09 16:19:00
23 2019-09-20 08:19:00
i want get the result consider above table record and then split time in to the two part .how can i do this task? actual requirement is tempattendancedata table data also need considering for the time split
you include the results of your temp table using UNION ALL then, put it in a subquery for your select case statement
SELECT *,COUNT(Time)
,CASE WHEN COUNT(Time) = 1 THEN
CASE WHEN Time > '00:00:00' && Time <= '12:15:00'
THEN Time END ELSE MIN(Time)
END as time_in,
CASE WHEN COUNT(Time) = 1 THEN
CASE WHEN Time > '12:15:00' && Time <= '23:59:59'
THEN Time END ELSE NULLIF(MAX(Time), MIN(Time))
END as time_out
FROM
(SELECT *
FROM attendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%'
UNION ALL
SELECT *
FROM tempattendancedata
INNER JOIN nonacadamic
ON attendancedata.EnrolledID = nonacadamic.emp_id
WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%') t1
GROUP BY t1.EnrolledID, t1.Date

Searching if a date is in an event

I have a db table with a list of events
e.g.
starteventX = 2019-02-20 endeventX = 2019-03-15
starteventY = 2019-03-11 endeventY = 2019-05-28
I need to know if year 2019 and month 04 are in the event so I can prepare a monthly report
I tried
$mainlink = mysqli_query($db,"SELECT * FROM eventi WHERE ((year1 >= '$year') OR (year2 <= '$year'))
AND ((month1 >= '$month') OR (month2 <= '$month')) ") or merror($msg = mysqli_error($db));
but cant get the required info
As a concept (assuming the data is valid, i.e. the end of some event is always later than its start):
If you want at least a day of a given month be in the event:
SELECT * FROM e
WHERE e.startevent <= '2019-04-30' AND e.endevent >= '2019-04-01'
If you want the entire month be in the event:
SELECT * FROM e
WHERE e.startevent <= '2019-04-01' AND e.endevent >= '2019-04-30'
If you want overlapping time intervals, then it is something like this:
SELECT e.*
FROM eventi e
WHERE e.startevent < '2019-05-01' AND -- starts before the end of the month
e.endevent > '2019-04-01' -- ends after the start of the month

Select depending CURDATE MySQL

I have the following query:
SELECT tbl_usuarios.Correo,
tbl_alertas.Id,
tbl_alertas.Purpose,
tbl_alertas.Status,
tbl_alertas.OpenDate,
tbl_alertas.CloseDate,
tbl_alertas.Owner,
tbl_alertas.ValueStream,
tbl_alertas.Family
FROM tbl_usuarios
INNER JOIN tbl_alertas ON tbl_usuarios.Nombre = tbl_alertas.Owner
WHERE (STATUS = 'En aprobacion' OR STATUS = 'Activa')
AND CloseDate < CURDATE()
As a result of this query I have the following table:
Everything it's ok.
Now, the problem is that I 'm going to use that query in my website, I create a JavaScript function which is going to be executed every 5th and every 19th of a month. My question, what can I do to only select the row which his CloseDate is <= yyyy-mm-07 or CloseDate >= yyyy-mm-07 and <= yyyy-mm-21 depending of CURDATE(5th or 19th), example:
Curdate = yyyy-mm-05.
Expected result: Data from Id 00003
What I tried was adding another AND to the query, something like this:
AND CloseDate < CURDATE() AND CloseDate <= '2016-09-07'
If my question is not much clear let me explain with this, I want to select CloseDate '2016-09-07' when curdate = '2016-whatever-05, 06', 07 or select CloseDate '2016-09-21' where curdate = '2016-whatever-19, 20, 21'.
Don't know if I understood your problem correctly but check the DAYOFMONTH() or simply DAY() function.
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
You can use it like this
SELECT tbl_usuarios.Correo,
tbl_alertas.Id, tbl_alertas.Purpose,
tbl_alertas.Status, tbl_alertas.OpenDate,
tbl_alertas.CloseDate,
tbl_alertas.Owner,
tbl_alertas.ValueStream,
tbl_alertas.Family
FROM tbl_usuarios
INNER JOIN tbl_alertas
ON tbl_usuarios.Nombre = tbl_alertas.Owner
WHERE (STATUS = 'En aprobacion' OR STATUS = 'Activa')
AND (CASE
WHEN DAY(CURDATE()) = '5' AND DATE(CloseDate) <= DATE_FORMAT(NOW() ,'%Y-%m-07')
THEN 1
WHEN DAY(CURDATE()) = '19' AND (DATE(CloseDate) >= DATE_FORMAT(NOW() ,'%Y-%m-07') AND DATE(CloseDate) <= DATE_FORMAT(NOW() ,'%Y-%m-21'))
THEN 1
ELSE 0
END)

mysql to check room is exists for a particular date with time duration

I have check room is exists between two time for particular date.
I have try following two query its run some time rights but, when i select 10:00 AM to 12:00 PM at time wrong results means not return any records.
QUERY-1 :
SELECT 1 FROM `timetable_details` WHERE (
((`td_from` <= '10:00:00') AND (`td_to` > '10:00:00'))
OR
((`td_from` < '12:20:00') AND (`td_to` >= '12:20:00'))
) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))
QUERY-2 :
SELECT 1 FROM `timetable_details` WHERE (
(`td_from` > '07:00:00') AND (`td_to` < '08:00:00')
) AND ((`td_room`='1') AND (`td_date`='2016-01-25'))
I have get td_id = 4 number row but is not returns.
You can use between with OR condition for both columns as below :
SELECT 1 FROM `timetable_details` WHERE (((((`td_from` BETWEEN '10:00:00' AND '12:30:00') OR (`td_to` BETWEEN '10:00:00' AND '12:30:00')) AND ((`td_room`='1') AND (`td_date`='2016-01-25') AND (`td_status` IS NULL))) AND (`td_from` <> '12:30:00')) AND (`td_to` <> '10:00:00'))

Calculate difference between first and last result

I have got a table with two columns. The first one ("val") is a integer, the second a timestamp ("ts").
Now I want to calculate the difference between the first and the last value of a given timespan.
SELECT MAX(val) - MIN(val) AS difference WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
This one is not sufficient, because in the course of time the values can exceed/undercut the first and the last value.
Example:
Day 1: 100
Day 2: 120
Day 3: 110
Day 4: 98
Day 5: 105
Day 6: 112
Day 7: 110
The difference is 110 (Day 7) minus 100 (Day 1) = 10. Not Max(val) = 120 minus Min(val) = 98 = 22
Thanks!
Join to a subquery that returns the first and last dates and join using those, and use some simple arithmetic to calculate the difference using sum():
select sum(val * case ts when ts1 then -1 else 1 end) diff
from data
join (select min(ts) ts1, max(ts) ts2
from data
where ts between ? and ?) x
on ts in (ts1, ts2)
See demo (demo schema has been simplified to isolate the essence of the solution).
One method is to use two subqueries, one that gets the first value and one that gets the last value:
SELECT ((SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val DESC
LIMIT 1
) -
(SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val ASC
LIMIT 1
)
) as difference