获取datetime的行,这是最新的日期时间小于mysql中的选定日期

问题描述:

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

我必须得到最新日期小于从选择框中选择的给定日期的行。 p >

我已经找到了如何获取最新日期的行,如 p>

select * from test_table where in created in(select max(created)from test_table) code> p>

我知道我必须使用 code>之间创建的,但我无法解决它。 从选择框中选择作为上限的日期。 所以,我手头之前不知道。 请帮助我 p>

例如,让从文本框中选择的日期是2013年11月11日。我必须得到一个创建日期的行,即 表格中的最大日期少于2013年11月11日。 谢谢提前 p> div>

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