如何編寫查詢以告訴我如果指定日期(e.g. 2015-01-15)
屬於下表中ANY'from_date
'to_date
'期間的範圍內?這裏有什麼特別的是我只需要返回一個ID。列與行之間的SQL檢查日期
id | from_date | to_date
a | 2015-01-01 | 2015-01-30
a | 2015-04-01 | 2015-04-30
目前查詢:
select id,
case when from_date <= '2015-01-15'
and to_date >= '2015-01-15'
then 'true' else 'false' end as status
from dates
當前的結果:
id | status
a | true
a | false
期望的結果:
id | status
a | true
你可以找到你所需要的:https://www.postgresql.org/docs/9.1/static/ functions-datetime.html – Sombriks
你有沒有試圖寫這個?這是一個非常基本的SQL請求。請查看[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)。關於Sombriks的建議,這是一個起點,但您需要的是'WHERE'子句中的[BETWEEN](http://www.postgresqltutorial.com/postgresql-between/)運算符。 –
謝謝約翰,試圖改進這個例子。 – Kieran