Query date in mysql?

1

I need to look up and display the birthdays of the week on the system dashboard, but today a birthday boy is not showing up and would like to know where the error is.

Anniversary column: 1970-02-15 (date is saved this way in bd), type date and nullable .

Query mysql: SELECT * FROM tbl_usuarios WHERE (WEEK(Aniversario) = WEEK(now()))

We had not noticed anything wrong, until the birthday boy himself questioned us, now we are in doubt if the same should be happening with other users.

    
asked by anonymous 15.02.2018 / 15:18

1 answer

2

The problem is that WEEK() returns a number between 0 and 53; according to the year, the day may not be in the same week (eg in one year the 6/01 day is in one week , in the other it is < strong> week two ).

One way would be to add years to the date of birth to check with the current date (using DATE_ADD() ):

SELECT * 
FROM tbl_usuarios 
WHERE (WEEK(DATE_ADD(Aniversario, INTERVAL (YEAR(now()) - YEAR(Aniversario)) YEAR)) = WEEK(now()))
    
15.02.2018 / 15:26