mysql: SELECTing rows X hours old to the nearest hour - mysql

I have a clause in mysql query to select with a timestamp of 3 days ago, to the nearest day:
WHERE TO_DAYS(wit_matches.created) = TO_DAYS(NOW() - INTERVAL 3 DAY))
I want to change this so that it selects rows with a timetamp of 3 days about, but to the nearest hour - i.e. 72 hours to the nearest hour (this is a cron job that will run ones per hour).
What's the best way of achieving this?

You can try this.
WHERE wit_matches.created BETWEEN (NOW() - INTERVAL 73 HOUR) AND (NOW() - INTERVAL 72 HOUR)

71.5 < 72 < 72.5 it is very easy once u get the hang of it

Related

sql query to fetch the records of next 30 days

Here is my problem, I want to fetch next 30 days records from the table. I have a field in my table. For ex: In my table I have resource_date, In this column I have many records from 2013-02-05 to 2015-10-10. Say, If I logged into the website today(Today's Date is- 16/01/2015, It should fetch record for next 15 days and so on). How to do this? Thanks in advance
One way to do it
SELECT *
FROM table1
WHERE resource_date >= CURDATE() + INTERVAL 1 DAY -- skip today
AND resource_date < CURDATE() + INTERVAL 17 DAY -- 15 days starting tomorrow
Here is a SQLFiddle demo
In MySQL, you can use the NOW() function to get the current DATETIME, and the INTERVAL keyword to get intervals of time.
So, to get the records where resource_date is within the next 30 days, you would use:
SELECT *
FROM `my_table_name`
WHERE `resource_date` >= NOW()
AND `resource_date` < NOW() + INTERVAL 1 MONTH
;
In practice, you should rarely use SELECT *, and you should consider adding a LIMIT to this query to prevent your application from returning a result set that is "too large".
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
...
WHERE
'resource_date'> NOW() AND
'resource_date'< DATE_ADD(NOW(), INTERVAL 31 DAY);
Careful I think now() does minutes and hours so you miss a portion of a day.
WHERE resource_date >= CURDATE() AND resource_date <= DATE_ADD(CURDATE(), interval 15 DAY)

Data from current date to 30 days after current days

Why this query is not working
SELECT * FROM history WHERE DATE(date) < CURDATE() + 30
I am trying to get the data from 30 days but my query is not working.Why
What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:
SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY
To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:
SELECT * FROM history WHERE
date >= CURDATE()
AND
date < CURDATE() + INTERVAL 31 DAY
Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.
The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.
Because you're not using the right construct, try:
SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);

MySQL "day" - how do I get an exact figure?

I am using this function to filter query results that are older than 60 days:
s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
The problem is, the "60 days" part doesn't seem to be an exact figure. I want it to filter right where s.timeSubmitted is longer than 60 days, down to the exact second of s.timeSubmitted.
How do I write "60 Days" as an exact figure (down to the second)?
The problem is that CURDATE() returns a DATE type, not a DATETIME type (an instant in time). The result of subtracting an interval from a DATE is also a DATE.
Instead, try this:
s.timeSubmitted >= ( NOW() - INTERVAL 60 DAY )
This gives you what you want, because NOW() returns a DATETIME, so the result of the subtraction is also a DATETIME.
INTERVAL 60 DAY is exact - your problem is that CURDATE() isn't. It returns whole days, not the current time.
Use NOW() instead!
I usually do
now()-interval 60 day
Assuming you want the same time of day 60 days ago;
s.timeSubmitted >= ( now() - interval 60 day);
Maybe an un-necessary note in this case; 1 day ago may be 23, 24 or 25 hours ago depending on DST changes, if you want a specific number of hours as an interval, don't use a day instead of 24 hours.

Check if timestamp is after 12 hours

I'm looking to display listings from my database where the row creation (which is a timestamp) is after 12 hours from "NOW".
I've found a lot of documentation on within a timeframe, e.g:
WHERE `date` <= DATE_SUB(now(), INTERVAL 12 HOUR)
But I can't figure out how to SELECT when a row is after 12 hours. (Keeping everything that was created within 12 hours not selected)
Any help would be really appreciated,
Thanks!
UPDATED
If you need only records that created more than 12 hours ago
select * from table where creation <= NOW() - INTERVAL 12 HOUR
If you need only records that created during last 12 hours
select * from table where creation >= NOW() - INTERVAL 12 HOUR
select * from table where creation > unix_timestamp() + 43200
Try to use:
WHERE date BETWEEN DATE_SUB(now(), INTERVAL 12 HOUR) AND now();

how do i get mysql rows from 24-48 hours ago?

I have a mysql table with a datetime column. I want to fetch rows which are older than 24 hours but younger than 48 hours. Ive gone through the date time function not quite sure how to approach this or what to do in general.
Use:
SELECT *
FROM YOUR_TABLE t
WHERE t.datetime_column < DATE_SUB(NOW(), INTERVAL 24 HOUR)
AND t.datetime_column > DATE_SUB(NOW(), INTERVAL 48 HOUR)