I have four dates with开发者_Python百科 a different prices for each 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'
PROBLEM: This query returns $25 and $50. I need it to give me the nearest date only...
How can i have it return only the $50?SELECT top 1 price FROM MyTable WHERE MyDate <= '10/12/2011' order by MyDate desc
Try this (in SQL Server)
SELECT TOP 1 price
FROM MyTable
WHERE myDate <= getDate()
ORDER BY myDate DESC
Try this (in mySQL)
SELECT price
FROM MyTable
WHERE myDate <= now()
ORDER BY myDate DESC
LIMIT 1
精彩评论