从两个日期之间的日期范围中选择数据 [英] Select data from date range between two dates

查看:136
本文介绍了从两个日期之间的日期范围中选择数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Product_Sales 的表,它拥有这样的数据

I have a table Named Product_Sales and it holds data like this

Product_ID | Sold_by | Qty | From_date  | To_date
-----------+---------+-----+------------+-----------
3          | 12      | 7   | 2013-01-05 | 2013-01-07
6          | 22      | 14  | 2013-01-06 | 2013-01-10
8          | 11      | 9   | 2013-02-05 | 2013-02-11

如果我要在两个日期之间选择销售数据,那么查询是什么日期范围?

Now what is the query if I want to select sales data between two dates from a date range?

例如,我想从 2013-01-03 中选择销售数据到code> 2013-01-09 。

For example, I want to select sales data from 2013-01-03 to 2013-01-09.

推荐答案

正如你所看到的,有两种方法也可以完成任务:

As you can see, there are two ways too get things done:


  • 征集所有可接受的选项

  • 排除所有错误选项

显然,第二种方法要简单得多(仅针对四种情况)。

Obviously, second way is much more simple (only two cases against four).

您的SQL将看起来像:

Your SQL will look like:

SELECT * FROM Product_sales 
WHERE NOT (From_date > @RangeTill OR To_date < @RangeFrom)

这篇关于从两个日期之间的日期范围中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆