简体   繁体   中英

How to select only future days counting from today's date?

I need a function that would only show future dates.

The problem goes like this "Select the patients who have booked advanced dates from today."

The selection code I have right now is this:

select Patients_code
     , max(Date) 
  from Patients
     , GPS 
 group 
    by Patients_code 
having max(Date) > '2016-05-11';

GPS is the booked times, where Date is. Also, there are bookings that are still in 2016 but after a month and the answer I get are only 2017 dates.

Any suggestions?

If you are in PHP you might want to build your SQL like so:

    <?php
    //ADD AN END DATE TO SPECIFY A RANGE BETWEEN TODAY & THE ENDING DATE    
    $endDateRange   = "2016-09-30";

    $sql =<<<SQL
    SELECT Patients_code, max(Date) 
    FROM Patients, GPS 
    WHERE (Date >= CURDATE() && Date =< '{$endDateRange}')
    GROUP BY Patients_code 
SQL;

Assuming your End Date is 2016-09-30 , this might boil down to:

    SELECT Patients_code, max(Date) 
    FROM Patients, GPS 
    WHERE (Date >= CURDATE() && Date =< '2016-09-30')
    GROUP BY Patients_code 

Here is another Flavour...all of which are saying the same thing:

<?php
    //DECLARE A VARIABLE TO HOLD NR. OF DAYS INTO THE FUTURE:
    $daysFromNow   = "120";     //120 DAYS INTO THE FUTURE - APPROX. 4 MONTHS FROM NOW.
    $sql =<<<SQL
    SELECT Patients_code, max(Date)
    FROM Patients, GPS
    WHERE ( DATEDIFF(MAX(Date), CURRENT_DATE()) < {$daysFromNow})
    GROUP BY Patients_code
SQL; 

Which also otherwise implies (assuming the Range is 120 Days into the Future ):

    SELECT Patients_code, max(Date)
    FROM Patients, GPS
    WHERE ( DATEDIFF(MAX(Date), CURRENT_DATE()) < 120)
    GROUP BY Patients_code

@kamal pal pointed out that you might not need the having Clause. Now, I don't pretend to know what you want to do but I think he may have have made a good point... although it all depends on you and what your intention really is. This part is yours to decide... ;-)

I figured it out! Thank you all for your help this was very helpful!

What I had to do:

select GPS.PK, Date, Name, Lastname from Patients, GPS where Date>NOW() and PatientsID=GPS.PK and PatientsID=645;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM