SQL日期比较
我收到了与此类似的查询,
select *
from stuff where stuff.id = 1
and start_Dt < = todays_date
and End_Dt > = todays_date
我问提供此信息的人为什么要进行日期比较,答案是“开始和结束日期对于确保唯一的记录匹配是必要的”
我很困惑,这样的比较不是吗等于任何可能的日期?
I was given a query similar to this
select *
from stuff where stuff.id = 1
and start_Dt < = todays_date
and End_Dt > = todays_date
I asked the person who gave it to me why the date comparison, the answer was "The start and end dates are necessary to ensure a unique record match"
I'm confused, wouldn't that comparison equate to any date possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因是,表中的 ID 不是唯一的,即您可以有多个带有
stuff.id = 1
的行。但在任何给定时间只有一个处于活动状态。这是通过日期比较进行检查的:它返回当前有效的
stuff.id = 1
行。该行的开始日期为过去日期 (start_Dt <= Todays_date
),结束日期为将来日期 (End_Dt >= Todays_date
)。The reason is, that the IDs in your table are not unique, i.e. you can have more than one row with
stuff.id = 1
.But only one at any given time is active. This is checked with the date comparison: It returns the row with
stuff.id = 1
that is currently valid. This is the row where the start date is in the past (start_Dt <= todays_date
) and the end date is in the future (End_Dt >= todays_date
).为了举例说明 daniel-hilgarth 的答案,您可能有一个包含随时间变化的费率的表。
此特定示例显示了英国的标准增值税税率。
因此,对于今天的汇率,这将返回这一行。
该系统的优点是您可以预先实现该表。因此,在 2009 年 12 月,它将使用 15.0 增值税,但一旦您的计算机日期达到 2010 年 1 月 1 日,就会自动更改为 17.5,然后在日期达到 2011 年 1 月 4 日时再次更改
。您还可以使用它来计划未来的价格上涨。
To give an example to illustrate daniel-hilgarth's answer, you may have a table containing a rate which changes overtime.
This particular example shows the standard VAT rate in the UK.
So for todays rate this will return this row
The advantage of this system is that you can implement this table beforehand. So in Dec 2009, it will use 15.0 Vat, but automatically changes to 17.5 once your computerdate reaches 1st Jan 2010 and then change again once the date reaches 4th Jan 2011.
You can also use it to plan future price increases.