0

I would like to write an SQL query which uses results from previous one.

For example - I have a table Orders with fields: order_id, date and value. I need to get all dates from column date where value is larger than 5:

SELECT date 
FROM Orders 
WHERE value > 5;

Then I need to return all values for dates, which are +2 days from the returned ones. Is it possible to write short query without using LOOP statement?

Here is an example table:

enter image description here

I am expecting to get the result:

enter image description here

3
  • Please provide sample data and expected results. Commented Feb 7, 2020 at 0:14
  • Also, which version of MySQL are you running? Commented Feb 7, 2020 at 0:14
  • dev.mysql.com/doc/refman/8.0/en/… Commented Feb 7, 2020 at 0:17

1 Answer 1

1

his will give you what you need.

select date, value  
from Orders where date in
(
  SELECT date + INTERVAL 5 DAY as date
  FROM Orders 
  WHERE value > 100;
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.