SQL WHERE 日期范围查询
在我的表中,有 118 条详细说明项目的记录。我在这里关心的两个字段是开始日期和结束日期。
我需要从此视图生成一份报告,显示哪些项目在以下日期范围内处于“活动”状态:
01/01/2011 - 01/12/2011
我尝试了以下 WHERE 类:
WHERE startdate BETWEEN '01/04/2011' AND '01/12/2011'
OR enddate BETWEEN '01/04/2011' AND '01/12/2011'
OR startdate <= '01/04/2011' AND enddate >= '01/12/2011'
出现的情况似乎不正确,仅显示了几条记录,而我知道应该显示的许多记录并未显示,例如一个项目的开始日期为 20/07/2011,结束日期为运行 WHERE 查询时,21/11/2011 会消失。
任何人都可以看到此 WHERE 查询的错误
In my table I have 118 records detailing projects. The 2 fields I am concerned with here are startdate and enddate.
I need to produce a report from this view which shows which projects were 'active' between the following date ranges:
01/01/2011 - 01/12/2011
I have tried the following WHERE clase:
WHERE startdate BETWEEN '01/04/2011' AND '01/12/2011'
OR enddate BETWEEN '01/04/2011' AND '01/12/2011'
OR startdate <= '01/04/2011' AND enddate >= '01/12/2011'
What comes through does not seem correct, there are only a few records displayed and many which I know for a fact should be displayed are not, such as one project with a start date of 20/07/2011 and enddate of 21/11/2011 dissapears when the WHERE query is run.
Can anyone see a fault with this WHERE query
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(假设 enddate 中的值是项目活动的最后日期)
使用数字的示例,搜索任何重叠 100 到 200 的内容...
这绝对需要逻辑中的 AND :)
就您的查询而言.. 。
带括号的查询看起来像这样...
但是您的示例永远不会满足最后一个 AND 条件。尝试添加括号以更明确...
(Assuming the value in enddate is the last date the project is active)
Examples using numbers, searching for anything that overlaps 100 to 200...
This absolutely needs an AND in the logic :)
In terms of your query...
Your query with parenthesis, looks like this...
But your example never meets the last AND condition. Try adding parenthesis to be more explicit...
在查询添加之前
还可以添加一些括号
Before the query add
Also maybe add some brackets
假设
startdate
和enddate
是日期字段,请尝试以下操作:
将开始日期声明为
01/01/2011
,将结束日期声明为2011年1月12日
Assuming
startdate
andenddate
are date fields,Try this:
Declaring your start date as
01/01/2011
and your end date as01/12/2011
当您可以将字符串与列进行比较时,每个人都会以错误的方式将开始日期和结束日期与字符串进行比较;以下是确定您想要什么的最简单方法:
Everyone's looking at this the wrong way round comparing startdate and enddate to a string when you can compare the string to the columns; the following is the simplest way of ascertaining what you want:
我的原始查询正常工作,但我连接的数据库与我的查询具有不同的日期格式。
My original query was working, the database I was connecting to however had different date formats to my query.