I'd like to build a query that updates daily at 1pm, which shows static data from 3pm to 11am nexy day.
if now is 2018-06-16 12pm, shows data from 2018-06-15 3pm - 2018-06-16 11am
if now is 2018-06-16 1pm, shows data from 2018-06-16 3pm - 2018-06-17 11am
Here is the query I tried.
UPDATED:
SELECT * FROM config WHERE gt BETWEEN CONCAT(curdate(),' 15:00:00') AND CONCAT(curdate()+ INTERVAL 1 DAY,' 10:59:59')
ISSUES:
When today is 2018-06-15, curdate() -> 2018-06-15, which shows data of today which no issues.
When today is 2018-06-16, curdate() -> 2018-06-16, which shows data of next day way too early, what i want is update next day 1pm daily.
Two cases:
It's between 0:00 and 12:59: you want yesterday 15:00 till today 11:00
It's between 13:00 and 23:59: you want today 15:00 till tomorrow 11:00
The query:
select *
from config
where
(
hour(current_time) between 0 and 12
and gt >= current_date - interval 9 hours -- yesterday 15:00
and gt < current_date + interval 11 hours -- today 11:00
)
or
(
hour(current_time) between 13 and 23
and gt >= current_date + interval 15 hours -- today 15:00
and gt < current_date + interval 35 hours -- tomorrow 11:00
);
lot of ways you can apply select command. here I mentioned the only 2 ways. this is useful for you.
SELECT * FROM config
WHERE gt BETWEEN #07/04/1996# AND #07/09/1996#;
select * from config where gt and created_at > (now()- interval 10 day)
You can use this sample to build your query:
SELECT *
FROM table_name
WHERE `date` >= NOW() - INTERVAL 10 DAY;
Here some information on how to work with INTERVAL.
And here is some useful information how to execute query at a particular time.
EDIT:
if now() = 2018-06-16 1pm, shows data from 2018-06-16 3pm - 2018-06-17
11am
Then you ca use this example:
SELECT *
FROM config
WHERE DATE(gt) BETWEEN DATE(NOW() + INTERVAL 2 DAY_HOUR) AND -- 2018-06-16 3pm
DATE(NOW() + INTERVAL 22 DAY_HOUR); -- 2018-06-17 11am
DEMO here
Note: the first DATE should be earlier as second DATE in BETWEEN statement. Otherwise it won't work.
Related
I am using the following query to generate employee report for every day
Employee Report For Yesterday (12:00 to 12:00)
SELECT * FROM emp WHERE DATE(created) = DATE(NOW() - INTERVAL 1 DAY);
Employee Report For 24 Hours from Current Time When Job is running
SELECT * FROM emp WHERE (created > DATE_SUB(NOW(), INTERVAL 1 DAY));
In both the reports i want to skip records from 10:00 am. to 11:00 a.m and 4:00 p.m to 5:00 p.m records on that day
Please help me on how to do it?
Thanks
It can look like
SELECT *
FROM emp
WHERE DATE(created) = CURRENT_DATE - INTERVAL 1 DAY
AND TIME(created) NOT BETWEEN '10:00' AND '11:00'
AND TIME(created) NOT BETWEEN '16:00' AND '17:00';
I'm trying to do a query in MYSQL selecting orders where the created_at datetime is between the previous 3:00 in the morning and the next 3:00 in the morning.
For example: if I make that query at 2020-03-31 11:00:00, it should show me all the orders where the created_at timestamp are between 2020-03-31 03:00:00 and 2020-04-01 03:00:00.
I've tried to make this by using this query:
select *
from "orders"
where "created_at" BETWEEN cast(curdate() as datetime) + interval 3 hour
and cast(curdate() as datetime) + interval 1 day + interval 3 hour
but that doesn't work correctly because if i execute this query between 0:00 and 3:00 in the morning (For example at 2020-03-31 00:30:00), it won't show anything, but it should show me all the orders where the created_at timestamp are between 2020-03-30 03:00:00 and 2020-03-31 03:00:00.
Any idea of how to do that? I've been trying to find a solution for 2 hours a i couldn't find the right query to do so.
I would do:
created at >= date(now()- interval 3 hour) + interval 3 hour
and created_at < date(now()- interval 3 hour) + interval 3 hour + interval 1 day
The logic is to offset the current date and time by 3 hours, remove the time component, and then add 3 hours.
Then you could do
select ...
where `created_at` BETWEEN DATE_SUB(concat(CURDATE(), ' 03:00:00'), INTERVAL 1 DAY)
AND concat(CURDATE(), ' 03:00:00');
user | completed
mike | 2016-07-10 19:00:00
john | 2016-07-11 08:00:00
I am trying to select all rows in a database where the row completed is NOT between 14:00 the previous day and the current day before 10:00. The script is designed to be run at 10:30 everyday
I've tried this
SELECT name FROM daily_tracking WHERE completed NOT BETWEEN now() - interval 1 day AND NOW() - INTERVAL 8 hour
you should use date_sub
SELECT name
FROM daily_tracking
WHERE completed NOT BETWEEN date_sub(NOW(), interval 1 day )
AND date_sub(NOW(), INTERVAL 8 hour)
I would not depend on the exact time when the script is being run. Instead, use arithmetic based on the current date:
SELECT dt.name
FROM daily_tracking dt
WHERE completed < date_sub(curdate(), interval (24 - 14) hour) or
completed > date_add(curdate(), interval 10 hour);
This will work on a given day, regardless of the time the script is run.
You can also write it this way, which I prefer...
SELECT dt.name
FROM daily_tracking dt
WHERE dt.completed BETWEEN CURDATE() - INTERVAL 14 HOUR AND CURDATE() + INTERVAL 10 HOUR;
I'm trying to make a query that will select all my users who's donor status is ending within 10 days.
As i only want to send the message once I want to select all the users who has their donor status ending between 10 and 9 days ahead.
The Donor end date is in this format: 0000-00-00 00:00:00
This is the query I'm currently working around with:
SELECT UserID FROM users_info WHERE donorEnd BETWEEN (NOW() + INTERVAL 10 DAY) AND (NOW() + INTERVAL 9 DAY)
I think you problem is that you are adding a time not a date: NOW() + INTERVAL 9 DAY = 2015-02-27 19:19 not 2015-02-27 00:00
Try use ADDDATE with CURDATE():
SELECT UserID FROM users_info WHERE donorEnd BETWEEN DATE_ADD(CURDATE(), INTERVAL 9 DAY) AND DATE_ADD(CURDATE(), INTERVAL 9 DAY)
So, I can do the following to get data from last week.
select * from table where week(date)=week(curdate())-1
Same for 2 weeks ago. But this fails if the data is in the prior year. What query can I use to get data from n weeks ago regardless of what year the data belongs to.
Edit: The week starts on Sunday 12AM and ends Saturday 11:59PM
When does a "week" start? Sunday? Monday? The same day of week as today?
Assuming you are happy with the last option, do this:
SELECT ...
WHERE date >= CURDATE() - INTERVAL $n WEEK
AND date < CURDATE() - INTERVAL $n-1 WEEK
Example
mysql> SELECT CURDATE(), CURDATE() - INTERVAL 9 WEEK, CURDATE() - INTERVAL 9-1 WEEK;
+------------+-----------------------------+-------------------------------+
| CURDATE() | CURDATE() - INTERVAL 9 WEEK | CURDATE() - INTERVAL 9-1 WEEK |
+------------+-----------------------------+-------------------------------+
| 2015-02-22 | 2014-12-21 | 2014-12-28 |
+------------+-----------------------------+-------------------------------+
If you need the week to start on a particular DOW, the query is messier, by further subtracting INTERVAL DAYOFWEEK(CURDATE()) DAY. And that could be off a little.
The start of the current week (assuming it is Sunday) is CURDATE() - INTERVAL (WEEKDAY(CURDATE() + INTERVAL 1 DAY)). So, replace CURDATE() in the above expression (twice) with this long mess.