How do I get the last 24hours in 24 rows from now? - mysql

For example my datetime is 2019/29/08 16:47
My table looks like this
ID stuff timestamp
1 ... 2019/29/08 16:47
2 ... 2019/29/08 16:45
3 ... ...
50 ... 2019/29/08 15:47
... ... ...
100 ... 2019/29/08 14:47
How do I select only the rows, that are 1h, 2h, 3h, .. ,24h before now?
to get something like
ID stuff timestamp
1 ... 2019/29/08 15:47
2 ... 2019/29/08 14:47
3 ... 2019/29/08 13:47
.. ... ...
24 ... 2019/28/08 16:47

SELECT *
FROM YourTable
WHERE timestamp IN ( NOW() - INTERVAL 1 HOUR,
NOW() - INTERVAL 2 HOUR,
NOW() - INTERVAL 3 HOUR,
NOW() - INTERVAL 4 HOUR,
NOW() - INTERVAL 5 HOUR,
NOW() - INTERVAL 6 HOUR,
NOW() - INTERVAL 7 HOUR,
NOW() - INTERVAL 8 HOUR,
NOW() - INTERVAL 9 HOUR,
NOW() - INTERVAL 10 HOUR,
NOW() - INTERVAL 11 HOUR,
NOW() - INTERVAL 12 HOUR,
NOW() - INTERVAL 13 HOUR,
NOW() - INTERVAL 14 HOUR,
NOW() - INTERVAL 15 HOUR,
NOW() - INTERVAL 16 HOUR,
NOW() - INTERVAL 17 HOUR,
NOW() - INTERVAL 18 HOUR,
NOW() - INTERVAL 19 HOUR,
NOW() - INTERVAL 20 HOUR,
NOW() - INTERVAL 21 HOUR,
NOW() - INTERVAL 22 HOUR,
NOW() - INTERVAL 23 HOUR,
NOW() - INTERVAL 24 HOUR)

Related

MySQL return value depending of date range

I don't understand why my SQL query doesn't work. I'm trying to do that :
if date is null then return 0
if date < 15 min then return 1
if date > 15 min and < 30 min then return 2
if date > 30 min and < 60 min then return 3
if date > 60 min and < 180 min then return 4
if date > 180 min then return 5
So here is my SQL query :
SELECT CASE
WHEN start_date IS NULL THEN 0
WHEN start_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE) THEN 1
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE) THEN 2
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL 60 MINUTE) THEN 3
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND DATE_SUB(NOW(), INTERVAL 180 MINUTE) THEN 4
ELSE 5
END as remaining_time
FROM table
But I don't understand why when I got only the cases 0, 1 and 5. remaining_time is never in the. cases 2, 3 ,4.
Do you know why ?
Such condition will never be met, because the lower bound is greater than the higher bound:
start_date BETWEEN DATE_SUB(NOW(), INTERVAL 15 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE)
You should have the bounds the other way around:
SELECT CASE
WHEN start_date IS NULL THEN 0
WHEN start_date < DATE_SUB(NOW(),INTERVAL 15 MINUTE) THEN 1
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 MINUTE) AND DATE_SUB(NOW(), INTERVAL 15 MINUTE) THEN 2
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 60 MINUTE) AND DATE_SUB(NOW(), INTERVAL 30 MINUTE) THEN 3
WHEN start_date BETWEEN DATE_SUB(NOW(), INTERVAL 180 MINUTE) AND DATE_SUB(NOW(), INTERVAL 60 MINUTE) THEN 4
ELSE 5
END as remaining_time
FROM table
CASE stops on the first matching condition, so this can be simplified as follows:
SELECT CASE
WHEN start_date is NULL THEN 0
WHEN start_date < NOW() - INTERVAL 15 MINUTE THEN 1
WHEN start_date < NOW() - INTERVAL 30 MINUTE THEN 2
WHEN start_date < NOW() - INTERVAL 60 MINUTE THEN 3
WHEN start_date < NOW() - INTERVAL 180 MINUTE THEN 4
ELSE 5
END as remaining_time
FROM table

Select rows from 6 am to 6 am

Help create a query that will select records from the table for the past period from 6AM until 6AM, the table has a timestamp field.
I will explain, for example:
Now: 2019-06-15 04:44:42
Period: 2019-06-13 06:00:00 - 2019-06-14 06:00:00
Now: 2019-06-15 07:44:42
Period: 2019-06-14 06:00:00 - 2019-06-15 06:00:00
Now: 2019-06-16 01:44:42
Period: 2019-06-14 06:00:00 - 2019-06-15 06:00:00
I think I was not the easiest request:
SELECT * FROM `table` WHERE `timestamp`
BETWEEN
DATE_ADD(DATE(NOW() - INTERVAL '1 6' DAY_HOUR), INTERVAL 6 HOUR)
AND
DATE_ADD(DATE(NOW() - INTERVAL '0 6' DAY_HOUR), INTERVAL 6 HOUR);
How simpler?
You can get the upper range limit with:
date(now() - INTERVAL 6 HOUR) + INTERVAL 6 HOUR
The lower limit would be exactly one day (24 hours) earlier:
date(now() - INTERVAL 6 HOUR) + INTERVAL 6 HOUR - INTERVAL 1 day
So your query could be:
SELECT *
FROM `table` t
WHERE t.timestamp >= date(now() - INTERVAL 6 HOUR) + INTERVAL 6 HOUR - INTERVAL 1 day
AND t.timestamp < date(now() - INTERVAL 6 HOUR) + INTERVAL 6 HOUR
But to avoid code duplication I would rewrite it to:
SELECT t.*
FROM `table` t
CROSS JOIN (
SELECT date(now() - INTERVAL 6 HOUR) + INTERVAL 6 HOUR as upper_limit
) r -- r for "range"
WHERE t.timestamp >= r.upper_limit - INTERVAL 1 day
AND t.timestamp < r.upper_limit
You can though use <= r.upper_limit instead of < r.upper_limit to get the same result as with BETWEEN.
SELECT start, start +interval 24 hour end
FROM ( SELECT date(now())
- interval if(hour(now())>6, 1, 2) day
+ interval 6 hour
as start
) dates;

SQL: Show "running" events

my script shows the events of the next 14 days:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
This works fine so far BUT at midnight all "running" (night)events are not displayed anymore.
So the query should output the events from the day before till 5 a.m.
This is my idea but it doesn't work:
SELECT
*
FROM
events
WHERE
eventdate
BETWEEN DATE_SUB(CURDATE(), INTERVAL 5 HOUR)
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)
Is it because 'eventdate' is a DATE-field?
Thanks!
It sounds like you want to subtract 5 hours before doing the comparison:
WHERE date_sub(eventdate, interval 5 hour) BETWEEN CURDATE() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
Alternatively, you can add 5 hours on the other side:
where eventdate between now() and datea_add(now(), interval 14*24 + 5 hour)
This should do the job or not!?
WHERE
eventdate
BETWEEN DATE(DATE_SUB(NOW(), INTERVAL 5 HOUR))
AND
DATE_ADD(NOW(), INTERVAL 14 DAY)

get 2 days ago query in mysql

So for getting last 24 hours query I use something like this
SELECT COUNT(*) AS cnt FROM `mytable` WHERE timestamp >= DATE_SUB(NOW(), INTERVAL 1 DAY)
where timestamp is a table field with timestamps.
but how can I get the interval between 2 days ago and yesterday.
So
today is 24 ian. I want a query between 22 ian (00:00am) and 23 ian (00:00am)
WHERE timestamp BETWEEN
DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)
You can also try DATE_ADD with a minus interval ;)
WHERE timestamp BETWEEN
DATE_ADD(DATE(NOW()), INTERVAL -2 DAY)
AND DATE_ADD(DATE(NOW()), INTERVAL -1 DAY)
Use Interval
WHERE `timestamp`
BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY)
AND DATE_SUB(NOW(), INTERVAL 1 DAY)
If you want a query between 22 Jan (00:00 AM) and 22 Jan (11:59 PM)
where DATE(timestamp) = DATE_SUB(DATE(now()), INTERVAL 2 day);
Example:
timestamp = 2020-02-24 12:07:19 and Date(timestamp) is 2020-02-24 and now() output is current date with time when we use DATE(now()) then output is Date only,
DATE_SUB(DATE(now()), INTERVAL 2 day)
is will be 2 days ago.
Try BETWEEN::
SELECT
COUNT(*) AS cnt
FROM `mytable`
WHERE timestamp BETWEEN DATE_SUB(NOW(), INTERVAL 2 DAY) and DATE_SUB(NOW(), INTERVAL 1 DAY)

Selecting results within diffrerent timesets in one sql query

How can select the last records added in 5 seconds, 5 minutes, 5 hours, 5 days, 5 weeks, 5 months, 5 years in one query?
Is it possible to be selected in one query?
sample records include
name | added_timestamp
v | last_five_minutes
k | last_hour
l | last_hour
the timestamps can be pseudo for the actual time
Although you haven't stated it, I suspect you want totals for the various time periods:
SELECT
SUM(date_added > NOW() - INTERVAL 5 SECOND) as total_in_last_5_seconds,
SUM(date_added > NOW() - INTERVAL 5 MINUTE) as total_in_last_5_minutes,
SUM(date_added > NOW() - INTERVAL 5 HOUR) as total_in_last_5_hours,
SUM(date_added > NOW() - INTERVAL 5 DAY) as total_in_last_5_days,
SUM(date_added > NOW() - INTERVAL 5 WEEK) as total_in_last_5_weeks,
SUM(date_added > NOW() - INTERVAL 5 MONTH) as total_in_last_5_months,
SUM(date_added > NOW() - INTERVAL 5 YEAR) as total_in_last_5_years
from records;
Edited: Added alternative prompted by comment
If you want the actual records, this will categorize them:
SELECT
*,
case
when date_added > NOW() - INTERVAL 5 SECOND then 'last_5_seconds'
when date_added > NOW() - INTERVAL 5 MINUTE then 'last_5_minutes'
when date_added > NOW() - INTERVAL 5 HOUR then 'last_5_hours'
when date_added > NOW() - INTERVAL 5 DAY then 'last_5_days'
when date_added > NOW() - INTERVAL 5 WEEK then 'last_5_weeks'
when date_added > NOW() - INTERVAL 5 MONTH then 'last_5_months'
when date_added > NOW() - INTERVAL 5 YEAR then 'last_5_years'
else 'ancient'
end as time_category
from records;
SELECT
*, /* now just don't be lazy but specify whatever columns you want to pull */
(Timestamp >= NOW() - INTERVAL 5 SECOND) AS AddedWithinSeconds,
(Timestamp >= NOW() - INTERVAL 5 MINUTE) AS AddedWithinMinutes,
(Timestamp >= NOW() - INTERVAL 5 HOUR) AS AddedWithinHours,
(Timestamp >= NOW() - INTERVAL 5 DAY) AS AddedWithinDays,
(Timestamp >= NOW() - INTERVAL 5 WEEK) AS AddedWithinWeeks,
(Timestamp >= NOW() - INTERVAL 5 MONTH) AS AddedWithinMonths
FROM atable
WHERE Timestamp >= NOW() - INTERVAL 5 YEAR
ORDER BY
AddedWithinSeconds DESC,
AddedWithinMinutes DESC,
AddedWithinHours DESC,
AddedWithinDays DESC,
AddedWithinWeeks DESC,
AddedWithinMonths DESC
Get the last records added in 5 years:
SELECT * FROM records WHERE date_added > NOW() - INTERVAL 5 YEAR
Then extract the records added in 5 seconds, 5 minutes, 5 hours, 5 days, 5 weeks, 5 months from the results.