[英]select a date that is lesser than today
我有一張下面的桌子。
id date value
12 2017-12-11 250
13 2017-12-11 256
14 2017-11-10 300
15 2017-11-09 400
我需要編寫一個查詢,以獲取今天的先前日期。 就像今天是2017年12月11日一樣,查詢應只返回
id date value
14 2017-11-10 300
請幫我如何得到這個。
因為最初的問題是用sql-server mysql postgresql標記的,所以我提供了3個版本的解決方案
SQL Server版本
您可以使用Convert(date, getdate())
來獲取今天的日期(沒有時間) ,並且通過簡單的查詢就可以獲取您想要的內容
SELECT TOP 1 *
From MyTable
Where [date] < Convert(date, getdate())
ORDER BY [date] desc
PostgreSQL版本
SELECT *
From MyTable
Where date < current_date
ORDER BY date desc
limit 1
MySQL版本
SELECT *
From MyTable
Where date < current_date
ORDER BY date desc
limit 1
select * from yourtable t where t.date_value < getdate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.