Fetch rows for the past x hours. The problem is that x can be any amount of hours.For instance if x is 48, I need to get all the rows for the past 2 days. How can I store the datetime
in the database and successfully retrieve the rows. Thank you. The user will select a date and time, then the duration(x).
You may use DATE_SUB() function in mysql.
*** WHERE `timecolumn_in_your_table` BETWEEN ( DATE_SUB( '2016-08-17', INTERVAL 48 HOUR ) ) and '2016-08-17'
conditions can be given as per your requirement
first you need to alter your table to add a datetime field
ALTER TABLE
MyTable
ADD timecolumn_in_your_table
DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Then, as Krishnakumar said, you can retrieve the rows using:
*** WHERE
timecolumn_in_your_table
BETWEEN ( DATE_SUB( '2016-08-17', INTERVAL 48 HOUR ) ) and '2016-08-17'
(the upper where clause selects rows with timecolumn_in_your_table
between 2016-08-15 00:00 and 2016-08-17 00:00)
(i wish i could add this post as a comment to Krishnakumar's answer, but i can not, as my reputarion rate is under 50)
yes Jones. above query will give answers between particular date and sub(time interval from that date ). I hope this was what you asked.