mySQL SELECT upcoming birthdays

前端 未结 17 581
我寻月下人不归
我寻月下人不归 2020-12-03 11:17

I\'m trying to write a query to select users of a database whose birthdays are in the next 7 days.

I\'ve done a lot of research but I can\'t come up with a working s

相关标签:
17条回答
  • 2020-12-03 11:39

    try this:

     Select * From persons
     where (DayOfYear(birthday) >= 7 
             And DayOfYear(birthday) - DayOfYear(curdate()) Between 0 and 6) Or
           (MOD(YEAR(curDate()),4) = 0) And MOD(YEAR(curDate()),100) != 0
            And (DayOfYear(birthday) + 366 - DayOfYear(curdate())) % 366 < 7) Or
             (DayOfYear(birthday) + 365 - DayOfYear(curdate())) % 365 < 7)
    
    0 讨论(0)
  • 2020-12-03 11:41

    Here is a simple PHP Code & SQL Query to retrieve upcoming birthdays. Where date of birth is stored as DATE (YYYY-MM-DD Format). enter image description here

    <?php
    $conn = mysqli_connect('localhost', 'user', 'paasword', 'databasename');
    $nod = 5;     //Number of days upto which you want to retrieve birthdays
    $delim = $filter = "";
    for($i=0;$i<$nod;$i++){
        $date = date_create(date('Y-m-d'));
        date_add($date,date_interval_create_from_date_string("$i days"));
        $filter .= $delim."'".date_format($date,"m-d")."'";
        $delim = ", ";
    }
    $sql = "SELECT NAME, DOB, MOBILE, EMAIL, TITLE FROM tablename WHERE SUBSTR(DOB,6) IN ($filter) ORDER BY SUBSTR(DOB,6) ASC";
    $result = mysqli_query($conn, $sql);    
    $i=0;
    echo '
    <table class="table-1 mt-2 table-responsive table-bordered">
        <tr>
            <th>S. No.</th>
            <th>Name</th>
            <th>DOB</th>
            <th>MOBILE</th>
        </tr>';
    while($row = mysqli_fetch_array($result)){
        $i++;
        $dob = date('Y-').date_format(date_create($row["DOB"]),'m-d');
        $day = date_format(date_create($dob),'w');
        switch ($day){
            case 0:
                $day = "Sunday"; break;
            case 1:
                $day = "Monday"; break;
            case 2:
                $day = "Tuesday"; break;
            case 3:
                $day = "Wednesday"; break;
            case 4:
                $day = "Thursday"; break;
            case 5:
                $day = "Friday"; break;
            case 6:
                $day = "Saturday"; break;
        }
        echo"
        <tr>
            <td>".$i."</td>
            <td>".$row["NAME"]."</td>
            <td>".date_format(date_create($row["DOB"]),'d-m-Y')." $day</td>
            <td>".$row["MOBILE"]."</td>
        </tr>
        ";
    }
    echo"
    </table>";
    ?>
    
    0 讨论(0)
  • 2020-12-03 11:42

    I did a lot "researches" and here is my solution, I guess it is very easy to understand!

    SELECT *,
    (366 + DAYOFYEAR(birth_date) - DAYOFYEAR(NOW())) % 366 as left_days
    FROM `profile`
    ORDER BY left_days;
    
    0 讨论(0)
  • 2020-12-03 11:43
    SELECT * from persons as p
    WHERE   
    MOD( DAYOFYEAR(p.bday) - DAYOFYEAR(CURRENT_DATE()) + 366, 366)  
    BETWEEN 0 and 7  
    ORDER by DAYOFYEAR(p.bday) ASC
    

    This works for me.

    0 讨论(0)
  • 2020-12-03 11:47

    I managed to get it working with this query. Due mostly to the help of Lobo's answer.

    SELECT * 
    FROM  persons 
    WHERE  DATE_ADD(STR_TO_DATE(birthday, '%m/%d/%Y'), INTERVAL YEAR(CURDATE())-YEAR(STR_TO_DATE(birthday, '%m/%d/%Y')) YEAR) 
                BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY);
    
    0 讨论(0)
  • 2020-12-03 11:48

    Any solution using DAYOFYEAR() will be flawed when the date of birth occurred during a leap year. Consider using TIMESTAMPDIFF() when working with age or birthdays.

    Current age can be calculated as

    timestampdiff(YEAR, person.date_of_birth, curdate())
    

    Calculate the upcoming birthday by adding the age to their date of birth.

    DATE_ADD(
        person.date_of_birth, 
        INTERVAL timestampdiff(YEAR, person.date_of_birth, curdate())+1 YEAR
    )
    

    Putting this together to solve the OP’s query

    select
        DATE_ADD(
            person.date_of_birth, 
            INTERVAL timestampdiff(YEAR, date_add(person.date_of_birth,INTERVAL 1 DAY), curdate())+1 YEAR
        ) upcoming_birthday
    from person
    having upcoming_birthday between curdate() and DATE_ADD(curdate(), INTERVAL 7 DAY)
    

    Notice that I have added one day to date_of_birth when calculating age, otherwise today’s birthdays would not be returned.

    0 讨论(0)
提交回复
热议问题