SQL 在两列之间选择日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7190568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Date Between Two Columns
提问by ConquistadorAravinth
I need a query in SQL.
If I have two columns STARTDATE
and END_DATE
.
I want to select a all rows where a date falls between these two dates.
我需要一个 SQL 查询。
如果我有两列STARTDATE
和END_DATE
.
我想选择日期落在这两个日期之间的所有行。
e.g.: startdate = 1/1/2011 AND enddate = 2/2/2011.
例如:startdate = 1/1/2011 AND enddate = 2/2/2011。
回答by Johan
SELECT * FROM table1
WHERE '2011-01-01' BETWEEN table1.startdate AND table1.enddate
Replace the explicit date either with now()
or a parameter or whatever.
用now()
或 参数或其他任何东西替换显式日期。
If the enddate is notdefined as NOT NULL
you can do:
如果结束日期未按NOT NULL
您的方式定义:
SELECT * FROM table1
WHERE '2011-01-01' BETWEEN table1.startdate AND COALESCE(table1.enddate, NOW())
回答by Jean-Charles
Does this help you ?
这对你有帮助吗?
select *
from table
where START_DATE < NOW() AND END_DATE > NOW()
Depending on the database, use CURRENT_TIMESTAMP() or TODAY()
根据数据库,使用 CURRENT_TIMESTAMP() 或 TODAY()
回答by John N
Do you mean this:
你的意思是:
select *
from mytable
where start_date >= '01/01/2011'
and end_date <= '02/01/2011'
Until you do more to clarify your question, it's hard for us to provide better answers.
在您进一步澄清问题之前,我们很难提供更好的答案。