I have to get the row with the latest date less than a given date selected from a select box.
I have figured out how to get the row with the latest date like
select * from test_table where created in (select max(created) from test_table)
I know I have to use a created between
but I have not been able to solve it. The date which is the upper limit is selected from a select box. SO, I dont know it before hand. Please help me
for example, let the date selected from textbox is Nov 11 2013. I have to get a row with a created date which is the max date in the table less than Nov 11 2013. Thanks in advance
This query will return the rows that have the latest datetime less than @givendate:
SELECT *
FROM test_table
WHERE created = (SELECT max(created)
FROM test_table
WHERE created < @givendate)
please notice that there could be more than one row with the same latest datetime. Or you could simply use this:
SELECT *
FROM test_table
WHERE created < @givendate
ORDER BY created DESC
LIMIT 1
To get the second last entry use the offset of the limit
function
select *
from test_table
order by created desc
limit 1,1
try this
select *
from test_table order by created desc
limit 1