Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

sql - 从两个日期之间的日期范围中选择数据(Select data from date range between two dates)

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

(我有一个名为Product_Sales的表,它包含这样的数据)

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?

(现在,如果我想从日期范围中选择两个日期之间的销售数据,该怎么办?)

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

(例如,我要选择2013-01-032013-01-09销售数据。)

  ask by Ronjon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

区间交点描述

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

(如您所见,有两种方法可以完成任务:)

  • enlist all acceptable options

    (征集所有可接受的选项)

  • exclude all wrong options

    (排除所有错误的选项)

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

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

Your SQL will look like:

(您的SQL将如下所示:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...