We have table named
Product_Sales
and it holds data like thisProduct_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
.
Answer is:
SELECT *
FROM Product_sales
WHERE From_date BETWEEN '2013-01-03' AND '2013-01-09'
OR To_date BETWEEN '2013-01-03' AND '2013-01-09'
OR From_date <= '2013-01-03'
AND To_date >= '2013-01-09'
Hint: If one of From_date
or To_date
is between the dates, or From_date
is less than start date and To_date
is greater than the end date.
Courtesy: FallenAngel (Stack OverFlow Ref.)