2

I have a MySQL table similar to this:

item | order | start date | end date ------------------------------------------ 1 1 2015-09-15 2015-09-20 2 1 2015-09-15 2015-09-20 1 2 2015-09-20 2015-09-25 2 2 2015-09-20 2015-09-25 

What I want to do is execute a query that will check if any end-dates are within 7 days of a future start date, and return the result. Does anyone know how this could be done?

EDIT: Should be more specific I suppose - the start date and end date of an order (say in this case order 2 from the example table) can be within 7 days of each other. I want to check if order 1's end date is within 7 days of order 2's start date. Sorry if that wasn't clear before.

1 Answer 1

4

You can use datediff function.

select * from table_name where start_date > curdate() and datediff(end_date,start_date) between 0 and 7 
Sign up to request clarification or add additional context in comments.

8 Comments

Won't that return a positive for order 2 in my example table?
Yes you should get the last 2 rows from the given example.
But does that give a positive even if order 1's end date was not within order 2's start date since order 2 has a start date and end date within 7 days of each other?
So you mean the check should be done on similar orders where the start date of the 2nd order is within 7 days of order 1's end date ? and if so is there a primary key in the table to define which one is the first order or which one is 2nd order ?
I mean to say - I want to check if order 1's end date is 7 days within order 2's start date, but I don't want to include if order 2's start and end dates are within 7 days of each other. I only want to see if order 1's end date is within 7 days of order 2's start date. Sorry if that wasn't clear before. EDIT: Should add that this should scale to 100s of orders.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.