Search code examples
mysqloracle-sql-data-modeler

Birthday with where clause


Can any one help me with this

Script

This is what i want to achive

table

id  name    tag_id  status      dob
== ======= ======== ========   ======== 
1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
3   c       2        Single     02/23/1960
4   d       1        Married    02/23/1985
5   e       2        Married    02/23/1999
6   f       2        Diovice    02/23/1999
7   g       1        Married    02/28/1999
8   h       2        Married    02/28/1999
9   i       1        Married    03/12/1990

I will love to get the present/ current birthday celebrant with mysql query here is my code:

SELECT * FROM table
WHERE (MONTH(STR_TO_DATE(dob, '%d/%m/%Y'))=MONTH(CURDATE())
    AND (DAY(STR_TO_DATE(dob, '%d/%m/%Y'))=DAY(CURDATE())))
    OR (DAY(LAST_DAY(STR_TO_DATE(dob, '%d/%m/%Y')))=29
    AND (DAY(STR_TO_DATE(bob, '%d/%m/%Y'))=29)
    AND (DAY(LAST_DAY(CURDATE()))=28))
    AND ( STATUS = 'Married' OR STATUS = 'Single' )
    AND tag_id = 1
ORDER BY id ASC

my problem is the above code is just returing all present bithday but what i want is like this:

result:

1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
4   d       1        Married    02/23/1985
7   g       1        Married    02/28/1999

Wrong result (produce by the above query):

1   a       1        Married    02/23/1977
2   b       1        Single     02/23/1978
4   d       1        Married    02/23/1985
7   g       1        Married    02/28/1999
8   h       2        Married    02/28/1999

And what i want, i only want Birthday Celebrant for today that/ where is ID = 1. Like i specify with the first result.


Solution

  • This later worked out for me, it might help others and i will love to see more improvement of this answer:

    SELECT * FROM table WHERE MONTH(STR_TO_DATE(dob, '%d/%m/%Y')) = MONTH(NOW()) AND DAY(STR_TO_DATE(dob, '%d/%m/%Y')) = DAY(NOW()) AND TAG_ID = 2 AND STATUS IN ('single','married')