MYSQL DATE RANGE and TIME RANGE - mysql

Counting fax completed by Graveyard Shift 10pm - 7am,
date and time are in different field
$stmt = $db -> prepare("SELECT count(*) FROM fax WHERE
date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY
and time >= '22:00:00' and time <= '7:00:00'
and shift='GY'
and complete=1");
$stmt -> execute();
echo $GY_COMP = $stmt -> fetchColumn();
This query always resulting to 0 but it has data.
Can someone help me with my query?
Thanks in advance :)

I think the problem was in your time comparison: you effectively filtered out everything with your time >= '22:00:00' and time <= '7:00:00'
Hope this helps: http://sqlfiddle.com/#!2/45108/7/0
SELECT * FROM fax
WHERE date BETWEEN CURDATE() and CURDATE() + INTERVAL 1 DAY
and
((time >= '22:00' and time <= '23:59')
or
(time >= '0:00' and time <= '7:00'))
and shift='GY'
and complete=1

Thanks Max for the time comparison,
I already made changes to my syntax
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59'))
or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00')))
and shift = 'GY'
and complete = 1
now my problem is RANGE of shift is today 10pm to tom 7am.
But my syntax above still get wrong when it change date
HELP IN USING IF STATEMENT in MYSQL
IF YES(SELECT THIS) ELSE IF (SELECT THIS)
HOW to properly use IF ELSE in MYSQL statement
SELECT count(*) FROM fax
IF
(date = CURDATE() and (time >='22:00' and time <= '23:59')
THEN
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >='22:00' and time <= '23:59'))
or (date=CURDATE() + INTERVAL 1 DAY and (time >= '00:00' and time <= '7:00')))
and shift = 'GY' and complete = 1
ELSE IF
(date = CURDATE() and (time >= '00:00' and time <= '7:00')
THEN
SELECT count(*) FROM fax
WHERE ((date = CURDATE() and (time >= '00:00' and time <= '7:00'))
or (date=CURDATE() - INTERVAL 1 DAY and (time >='22:00' and time <= '23:59')))
and shift = 'GY' and complete = 1

Related

MySQL Query Comparing DateTime not give the right row

dosen_schedule_datetimestart , dosen_schedule_datetimeend , academic_schedule_datetimestart , academic_schedule_datetimeendis formatted in DATETIME Format YYYY-MM-DD HH:MM:SS
This was my MySQL Query, so what i want to do is SELECT Row that Today is still in between datetimestart and datetimeend And TIME of datetimestart is less than 15 minutes compared to current time
But it doesnt work, and i also comparing academic_schedule_dow with Today's DOW
Any Idea?
$checkdosenschedule = mysqli_query($conn, "SELECT dosen_schedule_table.*, dosen_table.*, TIME(dosen_schedule_table.dosen_schedule_datetimestart) AS dosen_schedule_checkstart FROM dosen_schedule_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = dosen_schedule_table.dosen_id
WHERE (dosen_schedule_status = 'Active' AND dosen_subs_id != '' ) AND ((DATE(dosen_schedule_datetimestart) >= CURDATE() AND DATE(dosen_schedule_datetimeend) >= CURDATE()) AND (TIME(dosen_schedule_datetimestart) > (NOW() - INTERVAL 15 MINUTE)))");
$checkacademicschedule = mysqli_query($conn, "SELECT academic_schedule_table.*, dosen_table.* FROM academic_schedule_table
INNER JOIN dosen_table
ON dosen_table.dosen_id = academic_schedule_table.dosen_id
WHERE ((academic_schedule_status = 'Active' AND dosen_subs_id != '' ) AND academic_schedule_dow = DAYOFWEEK(NOW())) AND ((DATE(academic_schedule_datetimestart) >= CURDATE() AND DATE(academic_schedule_datetimeend) >= CURDATE()) AND (TIME(academic_schedule_datetimestart) > (NOW() - INTERVAL 15 MINUTE)))");
Try using DATE(NOW()) instead of CURDATE()
Also you are checking the range with same sign of greater then equal to.
If you want to check in between, use >= for start and <= for end.

Select by month sql

i have this date 2021-08-19,
i want to select by month all my data from my table where dateC = ( extract month).
EXEMPLE QUERY
SELECT * FROM MYTABLE WHERE dateC= extract month "08" from(2021-08-19)
You can use:
where year(dateC) = year('2021-08-19') and
month(dateC) = month('2021-08-19')
If you just want the month regardless of year, then:
where month(dateC) = month('2021-08-19')
Or
where date_format(dateC, '%Y%m') = date_format('2021-08-19', '%Y%m')
Neither of these are index friendly. That requires a little more work. Let met call your date #date:
where dateC >= #date + interval (1 - day(#date)) day and
dateC < (#date + interval (1 - day(#date)) day) + interval 1 month
Note: all of the above work if there is a time component. If dateC is really a date with no time component, then:
where dateC > last_day(#date - interval 1 month) and
dateC <= last_day(#date)

mysql select between two dates has odd behavior

I am selecting all records between NOW() and specific X day interval and came across this odd behavior that I don't understand.
I am checking 24 hours into the future and 24 hours into the past:
select * from table where date between NOW() and NOW() + 1 interval day; //works
select * from table where date between NOW() and NOW() - 1 interval day; //no records
But if I reverse the between call:
select * from table where date between NOW() + 1 interval day AND NOW(); //no records
select * from table where date between NOW() - 1 interval day AND NOW(); //works
Why does one call into the future work, but the same call into the past not work?...and if I reverse between parameters, the opposite behavior happens - does not work 24 hours into the future but does work 24 hours into the past.
======================
Adding #TimBiegeleisen explanation below here written out:
date = '2018-05-30' ;
select * from table where date between NOW() and NOW() + 1 interval day;
= date >= '2018-05-30' AND 'date <= 2018-05-31'; //true
select * from table where date between NOW() and NOW() - 1 interval day; records
= date >= '2018-05-30' AND 'date <= 2018-05-29'; //false
AND
select * from table where date between NOW() + 1 interval day AND NOW();
= date >= '2018-05-31' AND date <= '2018-05-30' //false
select * from table where date between NOW() - 1 interval day AND NOW();
= date >= '2018-05-29' and date <= '2018-05-30'; //true
The BETWEEN operator is interpreted a certain way:
WHERE date BETWEEN a AND b
means this:
WHERE date >= a AND date <= b
So the following two queries are equivalent:
select * from table where date between NOW() and NOW() - interval 1 day;
select * from table where date >= NOW() and date <= NOW() - interval 1 day;
Hopefully you can see that in your second query the WHERE condition can never be true, because a date cannot simutaneously be greater than or equal to now and less than now minus one at the same time.
simply put,
For SQL:
WHERE x between a and b
meaning
x >= a
and
x <= b
therefore, we have a <= x <= b or a <= b
PS: it's just about math :)

MYSQL Select by this week with index

User can see what they have uploaded in this week. I have below code in line:
Is this correct?
SELECT *
FROM images
WHERE userid = '$userid'
AND uploadeddate >= CURDATE() - INTERVAL WEEKDAY(day) DAY
AND uploadeddate < CURDATE() - INTERVAL WEEKDAY(day) DAY + INTERVAL 7 DAY
ORDER BY uploadeddate DESC
I have create index for (userid, uploadeddate) .
For your original query to work day needs to be in the database you're querying (or set as a variable) and it needs to be in a MySQL date '0000-00-00' or date time '0000-00-00 00:00:00' formats.
WEEKDAY() returns 0-6 for the date entered.
$first; //is earliest offset in the range
$last; //is the latest offset in the range
SELECT * FROM images WHERE userid = '$userid'
AND uploadeddate > CURDATE() - INTERVAL $first DAY
AND uploadeddate < CURDATE() - INTERVAL $last DAY
ORDER BY uploadeddate DESC
So a query like this would return results for last week.
SELECT * FROM images WHERE userid = '$userid' AND
uploadeddate > CURDATE() - INTERVAL 14 DAY
AND uploadeddate < CURDATE() - INTERVAL 7 DAY
ORDER BY uploadeddate DESC

Get yesterday query from MySQL

I'm trying to get queries just for yesterday! With this code, I can get queries for today, but I don't know what should i do to show just yesterday queries:
$t_date = date( 'Y-m-d H:i', $_TIME - (3596 * 24) );
$twall = $db->super_query("SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date > = '$t_date'
AND date < = '$t_date' + INTERVAL 24 HOUR
AND moder = '0' ");
$twall = $twall['c'];
Try this:
SELECT COUNT(*) as c
FROM dle_photo_post
WHERE date between
date_sub(curdate(), interval 2 day)
and date_sub(curdate(), interval 1 day)
AND moder = '0'