V
V
Vasily Alibabaevich2018-02-24 14:22:14
MySQL
Vasily Alibabaevich, 2018-02-24 14:22:14

How to select current date + 5 days from database in SQL?

Hello gentlemen!
Tell me please.
It is necessary to select only those users who have an appointment for 5 days in advance.
Here is the request itself:

SELECT patients.fio, patients.number_phone, 
    appointment.date_of_appointment, 
    appointment.time_of_appointment 
    FROM appointment
    INNER JOIN patients 
    WHERE DATE_ADD(CURDATE(), INTERVAL 5 day) >= appointment.date_of_appointment
    AND patients.user_id = appointment.user_id;

But all records that are older and younger than the current date are displayed.
How to correctly build a query so that records are shown according to the currentDate + 5 days principle?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DS28, 2018-02-24
@dimitrius86

Well, you are still doing the right thing)) But not completely.
CURDATE+5 is greater than date_of_appointment
This means date_of_appointment is less than CURDATE+ 5.
But how to cut off the past days? We also need date_of_appointment > CURDATE

D
d-stream, 2018-02-24
@d-stream

Well, the condition, if it is more carefully expressed in words, should sound like
"located between the current date and the date 5 days more",
that is, at least two comparisons or between
p / s / it is usually more convenient to operate with something like datediff - yes the difference in days and there you can more clearly see what you need " from zero to 5 days"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question