MySQL Query Comparing DateTime not give the right row - mysql

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.

Related

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 DATE RANGE and TIME RANGE

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

Cannot spot the difference between these two queries

Trying to clean up a long query I have come up with a modified version, but now the old query with certain parameters returns 6 rows, but my version returns none. I have been straining my eyes to spot the difference but cannot find any. Here are the WHERE clauses from the two versions with identical set of parameters:
The ugly (but working) version:
SELECT ...
FROM table as a
WHERE 1=1
and a.title LIKE '%Manager%'
and a.status='Approved'
and (a.effected_date<=now())
and (
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=0)
or
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=1)
)
My cleaned (but broken) version:
SELECT ...
FROM `table` AS `a`
WHERE CONCAT(`a`.`title`, ' | ', `a`.`job_detail_section`) LIKE '%Manager%' AND
`a`.`status` = 'Approved' AND
`a`.`effected_date` <= '2013-12-30' AND
(`a`.`effected_date` >= '2013-11-30' OR `a`.`is_hotjob` = '1')
You got the last AND/OR backwards:
`a`.`effected_date` <= '2013-12-30' OR
(`a`.`effected_date` >= '2013-11-30' AND `a`.`is_hotjob` = '1')
EDIT: after reformatting, the answer looks more like:
SELECT ...
FROM `table` AS `a`
WHERE CONCAT(`a`.`title`, ' | ', `a`.`job_detail_section`) LIKE '%Manager%' AND
`a`.`status` = 'Approved' AND
`a`.`effected_date` <= now() AND
`a`.`effected_date` >= DATE_ADD(a.effected_date, INTERVAL 30 DAY)
and POSSIBLY:
AND `a`.`is_hotjob` IN (0,1)
As a substitute of-
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=0)
or
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=1)
You could use the following-
(`a`.`effected_date` >= '2013-11-30' AND `a`.`is_hotjob` in (0, 1))
But according to the additional info i.e. a.is_hotjob has value either 0 or 1 then there is no need to mention it in the condition at all. So basically the following format will be sufficient-
`a`.`effected_date` >= '2013-11-30'

sql gives no results because empty left join

So I have this query:
SELECT
AED.aId, CJ.*
FROM
AED
LEFT JOIN
Cronjob as CJ
ON CJ.aID = AED.aId
WHERE
AED.aStatus = '1'
AND
(
CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND
AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR
(
CJ.cjStatus = '9'
OR
CJ.cjStatus = '2'
)
The problem is, the Cronjob table is empty, and if it's empty is still want to give all the Id's from AED with the status 1
I couldn't find anything use full, so I hope you guys can help!
You should move all the CJ criteria into the join's ON clause.
ON (
CJ.aID = AED.aId
AND (cjStatus in ('2','9') OR cjDatum < CURRENT_DATE - INTERVAL 14 DAY ))
Option two would be to leave them in WHERE, but make provisions for the case that cjStatus and friends are NULL (which they will be if no match is found).
OR cjStatus IS NULL
When there is no CronJob associated, the filter CJ.cjStatus = '9' (for example) return false, since CJ.cjStatus is null. That's what a LEFT JOIN do, it returns null field when there is no correspondance.
To add filter on the table you want to LEFT JOIN with, the filter clause must be in the join clauses like this:
SELECT AED.aId
, CJ.*
FROM AED
LEFT JOIN Cronjob as CJ
ON CJ.aID = AED.aId
AND (CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR (CJ.cjStatus = '9' OR CJ.cjStatus = '2')
WHERE AED.aStatus = '1'
Add OR (CJ.aid is null) to your AND part of condition:
SELECT
AED.aId, CJ.*
FROM
AED
LEFT JOIN
Cronjob as CJ
ON CJ.aID = AED.aId
WHERE
AED.aStatus = '1'
AND
(
(
CJ.cjDatum < CURRENT_DATE - INTERVAL 14 DAY
AND
AED.aRegistratie > CURRENT_DATE - INTERVAL 10 YEAR
)
OR (CJ.aid is null)
)
OR
(
CJ.cjStatus = '9'
OR
CJ.cjStatus = '2'
)

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'