获取最近/最低日期SQL

时间:2011-10-12 16:58:51

标签: sql date

我有四个日期,每个日期的价格不同:

10/01/2011  $25
10/08/2011  $50
11/17/2011  $100
12/23/2011  $150

SQL:

SELECT price FROM MyTable WHERE MyDate <= '10/12/2011'

问题:此查询返回$ 25和$ 50。我需要它才能给我最近的日期...
我怎么能只返回50美元?

2 个答案:

答案 0 :(得分:2)

SELECT top 1 price FROM MyTable WHERE MyDate <= '10/12/2011' order by MyDate desc

答案 1 :(得分:1)

试试这个(在SQL Server中)

SELECT TOP 1 price
FROM MyTable
WHERE myDate <= getDate() 
ORDER BY myDate DESC

试试这个(在mySQL中)

SELECT price
FROM MyTable
WHERE myDate <= now()
ORDER BY myDate DESC
LIMIT 1