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();
Related
I need to get data within a range which starts a hour ago. As an example if now time is 08.00 AM, I need to get all the data within 07.00 AM to last 10 days. I have tried with datesub function as below, But seems this is wrong because the date(now()) and date(now()-interval 1 hour) are both same.. Can someone show me how to get this?
.....and date(time)>date(date_sub((now()-interval 1 hour), interval 10 day)) group by ds.....
Assuming time field in your query is either timestamp or datetime.
... and time between cast(concat(date_sub(curdate(), INTERVAL 10 day), ' 00:00:00') as datetime) and DATE_SUB(now(), INTERVAL 1 hour)
Hope this will help.
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)
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);
Is there a way in a MySQL statement to order records (through a date stamp) by >= NOW() -1 so all records from the day before today to the future are selected?
Judging by the documentation for date/time functions, you should be able to do something like:
SELECT * FROM FOO
WHERE MY_DATE_FIELD >= NOW() - INTERVAL 1 DAY
Be aware that the result may be slightly different than you expect.
NOW() returns a DATETIME.
And INTERVAL works as named, e.g. INTERVAL 1 DAY = 24 hours.
So if your script is cron'd to run at 03:00, it will miss the first three hours of records from the 'oldest' day.
To get the whole day use CURDATE() - INTERVAL 1 DAY. This will get back to the beginning of the previous day regardless of when the script is run.
Didn't see any answers correctly using DATE_ADD or DATE_SUB:
Subtract 1 day from NOW()
...WHERE DATE_FIELD >= DATE_SUB(NOW(), INTERVAL 1 DAY)
Add 1 day from NOW()
...WHERE DATE_FIELD >= DATE_ADD(NOW(), INTERVAL 1 DAY)
You're almost there: it's NOW() - INTERVAL 1 DAY
Sure you can:
SELECT * FROM table
WHERE DateStamp > DATE_ADD(NOW(), INTERVAL -1 DAY)
when search field is timestamp and you want find records from 0 hours yesterday and 0 hour today use construction
MY_DATE_TIME_FIELD between makedate(year(now()), date_format(now(),'%j')-1) and makedate(year(now()), date_format(now(),'%j'))
instead
now() - interval 1 day
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)