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

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)

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)

mysql: SELECTing rows X hours old to the nearest hour

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

mysql get records greater than 10 hours

I have a time stamp in my table(2012-04-04 20:44:53) and i would like to select all of the records that are greater than 10.5 hours from the current time. Thanks for any help
SELECT ...
FROM ...
WHERE yourtimestampfield <= (NOW() - INTERVAL 10.5 HOUR)

MySQL select a date between two dates using the time now

I am trying to select a row from a table assuming that the kick off time is within an hour's range of the current time.
The table is just an id and a datetime field.
SELECT * FROM kick_offs WHERE NOW() BETWEEN (DATE_SUB(`time`, INTERVAL 30 MINUTES)) AND (DATE_ADD(`time`, INTERVAL 30 MINUTES))
SELECT * FROM kick_offs WHERE `time` BETWEEN (DATE_SUB(NOW(), INTERVAL 30 MINUTES)) AND (DATE_ADD(NOW(), INTERVAL 30 MINUTES))
These two queries both fail. I'm not really sure why. The server is running MySQL 5.0. What am I doing wrong?
Not sure where you got your SQL from, but this should do it I think.
SELECT * FROM kick_offs WHERE `time` < DATE_ADD(NOW(), INTERVAL 1 HOUR) AND `time`>= NOW()
See:
Display rows from MySQL where a datetime is within the next hour

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();