What's wrong with my date range in this SQL statement? - mysql

I'm attempting to select data in a date range, but this is not working:
SELECT form_user.email
FROM form_instance JOIN
form_user
ON form_instance.form_user_id = form_user.form_user_id
WHERE form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11' AND
form_instance.status = 3

this is the issue
form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11'
this should be
form_instance.appointment_date >= '2017-04-13' AND form_instance.appointment_date <= '2017-05-11'
or use BETWEEN

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.

How can I create this query?

I have this MySQL Query here:
SELECT
COUNT(*) ReleasePerMonth,
d.name as DevGroup_REGION
FROM
release_summary r
inner join
gti_server_info g
on r.gti_server_id = g.gti_server_id
inner join
dev_group d
on d.dev_group_id = g.dev_group_id
WHERE
r.testingFinishedOn_timestamp >= '2020-05-01 00:00:00'
AND r.testingFinishedOn_timestamp <= '2020-05-31 00:00:00'
AND r.test_type != 14
GROUP BY
d.name ;
Now, I want this to run for every month. That is,
r.testingFinishedOn_timestamp >= '2019-01-01 00:00:00'
AND r.testingFinishedOn_timestamp <= '2019-01-31 00:00:00'
and
r.testingFinishedOn_timestamp >= '2019-05-02 00:00:00'
AND r.testingFinishedOn_timestamp <= '2019-02-31 00:00:00'
Till end of year. Currently, I am doing this manually. Is there any way I can do it in an automated manner?
Clarification:
I'd want 12 seperate tables for each of the 12 months.
Try the following:
SELECT
YEAR(r.testingFinishedOn_timestamp) year,
MONTH(r.testingFinishedOn_timestamp) month,
COUNT(*) ReleasePerMonth,
d.name as DevGroup_REGION
FROM
release_summary r
inner join
gti_server_info g
on r.gti_server_id = g.gti_server_id
inner join
dev_group d
on d.dev_group_id = g.dev_group_id
WHERE
r.test_type != 14
GROUP BY
YEAR(r.testingFinishedOn_timestamp),
MONTH(r.testingFinishedOn_timestamp),
d.name
ORDER BY year, month;
A possible solution is to query data for the entire year and group by MONTH(testingFinishedOn_timestamp).
I added the query below but it's not tested:
SELECT
MONTH(r.testingFinishedOn_timestamp) ReleaseMonth,
COUNT(*) ReleasePerMonth,
d.name as DevGroup_REGION
FROM
release_summary r
inner join
gti_server_info g
on r.gti_server_id = g.gti_server_id
inner join
dev_group d
on d.dev_group_id = g.dev_group_id
WHERE
r.testingFinishedOn_timestamp >= '2020-01-01 00:00:00'
AND r.testingFinishedOn_timestamp < '2021-01-01 00:00:00'
AND r.test_type != 14
GROUP BY
d.name, MONTH(r.testingFinishedOn_timestamp);
ORDER BY
MONTH(r.testingFinishedOn_timestamp), d.name
Based on the documentation available, MONTH() function returns the number of the month, for instance for January returns 1.
If you want to have the name of the month you case use MONTHNAME() function instead of Month().
You can try the below query - using last_day()
SELECT year(r.testingFinishedOn_timestamp),month(r.testingFinishedOn_timestamp),
COUNT(*) ReleasePerMonth
FROM
release_summary r
inner join
gti_server_info g
on r.gti_server_id = g.gti_server_id
inner join
dev_group d
on d.dev_group_id = g.dev_group_id
WHERE
r.testingFinishedOn_timestamp >= date_add(date_add(LAST_DAY(now()),interval 1 DAY),interval -12 MONTH)
AND r.testingFinishedOn_timestamp <= LAST_DAY(now())
AND r.test_type != 14
GROUP BY
year(r.testingFinishedOn_timestamp),month(r.testingFinishedOn_timestamp) ;

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 Syntax error in UNION ALL

I have a query that uses multiple UNION ALL. I'm getting a syntax error and I can't find it. I think it has to do with using multiple unions. I'm only using MySQL. Any help is appreciated.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALL(SELECT a.' at line 41
SELECT Sum(counter) AS counter,
DATE_FORMAT(dtime,'%Y-%m-%d 00:00:00') AS dtime
FROM (
SELECT counter,
dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='980371'
AND counter != 0
UNION ALL
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='979661')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='979661' ))
ORDER BY dtime
AND counter != 0
UNION ALL (this is line 41)
(
SELECT a.counter,
a.dtime
FROM MinutesVisible a,
calendar b
WHERE a.dtime = b.dtime
AND b.dtime >= '2015:3:1 00:00:00'
AND b.dtime <= '2015:3:16 23:59:59'
AND b.dtime < NOW()
AND id='984121')
UNION
(
SELECT '0' AS counter,
b.dtime
FROM calendar b
WHERE dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND dtime NOT IN
(
SELECT dtime
FROM MinutesVisible where dtime >= '2015:3:1 00:00:00'
AND dtime <= '2015:3:16 23:59:59'
AND dtime < NOW()
AND id='984121' ))
ORDER BY dtime
AND counter != 0) AS MainTable
GROUP BY WEEKOFYEAR(dtime)
HAVING SUM(counter) > 0
If I change the 'id=' to id in and include the other id number there and drop the second union all it works. I'm pretty sure it has something to do with the second union all. This statement is built dynamically so I cannot easily change to using in instead of multiple union alls.
Let me know if I'm not explaining this clearly and I'll provide more detail.
check your statement just above the line where you've written (this IS line 41)
Try to remove/update this statement and execute your query.
ORDER BY
dtime
AND counter != 0
we can't use order in between the UNION & UNION ALL. Order by should be at the very last, after all UNION / UNION ALL statements

How can I make a query for each month 7:15-12:15 in MySQL?

I found two similar answers
SELECT Count(*)
FROM fd_member
WHERE From_unixtime(Unix_timestamp(add_time), '%h') >= 8
AND From_unixtime(Unix_timestamp(add_time), '%h') < 12
GROUP BY CONVERT(add_time, CHAR(10))
SELECT *
FROM fd_member f
WHERE Date(f.date) >= Date('2013-10-01')
AND Date(f.date) < Date('2013-11-01')
AND Hour(f.date) >= Hour('8:00:00')
AND Hour(f.date) < Hour('12:00:00')
But they are not accurate to the minute.
SELECT *
FROM fd_member f
WHERE Date(f.date) >= Date('2013-10-01')
AND Date(f.date) < Date('2013-11-01')
AND Time(f.date) >= Time('7:15:00')
AND Time(f.date) < Time('12:15:00')
Should work. Give it a go. if not could you provide an SQL fiddle with some sample data and what you expect out