简体   繁体   中英

How to get friends who have birthday today or in the next 7 days (week)?

before creating this topic I researched the entire community, but I did not find anything close to what I'm trying to do. I am developing a small social network which is a PHP project for academic purposes only.


I have the following tables in my database:

 Table Name: users Columns: id => INT (Primary Key - AutoIncrement) name => VARCHAR(200) birthdate => DATE login => VARCHAR(60) password => VARCHAR(60) Table Name: friends Columns: id => INT (Primary Key - AutoIncrement) idRequester => INT (Foreign Key - users>>id) requestDate => DATE idRequested => INT (Foreign Key - users>>id) confirmationDate => DATE situation => CHAR(1) (A=Accepted | P=Waiting | R=Rejected) 


With the following query, I can get all the birthdays of the day (Without considering friendships).

 SELECT id, name, DATE_FORMAT(birthdate, '%d/%m/%Y') AS dtbirth, TIMESTAMPDIFF(YEAR, birthdate, NOW()) AS age FROM users WHERE birthdate LIKE '%-06-21'; 


This is similar to a question raised in another topic here of the portuguese forum, in this link: Here

I need to get all the friends who have birthday today or in the next 7 days given the current date, this from a particular user X. I don't know how to JOIN tables users and friends because we have two columns and if X is the requesting user, then I need to join with the requested one, otherwise X being requested, then I join with the requestor.

That is, get all 'user ID 50' friends who have birthday today or in the next 7 days.


If someone can help me, because I'm not sure how to perform a query that solve this problem and also gains in performance. I believe that it would help a lot of people, since the doubt is something frequent and for academic purposes. Thank you.

Hi as per my understanding you require all the friends who're having their birthday's between today and next week for a particular user_id, and you also have confusion with how to pull all the friends since sometimes X person is the one who requested for friendship and sometimes X is the person who was requested for friendship.

I wrote below query, I hope it helps.

select ur.*, TIMESTAMPDIFF(YEAR, birthdate, NOW()) AS age 
 from users ur 
 inner join
 (
    -- getting all the friends who are accepted and the user was requested
   (
      select f.idRequester as friends_id
      from users u
      inner join friends f
      on (u.id=f.idRequested)
      where u.id=103 and situation = 'A'
   )
   union
   (
     -- getting all the friends who are accepted and the user was requester

     select f.idRequested as friends_id
     from users u
     inner join friends f
     on (u.id=f.idRequester)
     where u.id=103 and situation = 'A'
    )
  ) temp
    on(ur.id=temp.friends_id)
    /*
       this part compares if the day of birth lies 
       between today or next 7 days. 
    */ 
     WHERE DATE(CONCAT_WS('-', YEAR(curdate()), MONTH(birthdate), 
           DAY(birthdate))) BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 7 DAY);

NOTE: I have hardcoded the user_id, to make it dynamic you can probably use a stored procedure with parameters and replace the hardcoded part with it.

TRY THIS : I assume that your birthdate column is holding data only not time and you have to use OR condition to satisfy one of two or both conditions true. It will return current date and 7th days birth date records

SELECT id, name, DATE_FORMAT(birthdate, '%d/%m/%Y') AS dtbirth,
TIMESTAMPDIFF(YEAR, birthdate, NOW()) AS age
FROM users 
WHERE (birthdate = CURDATE() OR  birthdate = DATE_ADD(CURDATE(), INTERVAL 7 DAY))

--Replace `WHERE` with below line to Return records from Current date to next 7 days
WHERE birthdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
SELECT id, name, DATE_FORMAT(birthdate, '%d/%m/%Y') AS dtbirth,
TIMESTAMPDIFF(YEAR, birthdate, NOW()) AS age
FROM users 
WHERE birthdate between  CURDATE() and  DATE_ADD(CURDATE(), INTERVAL 7 DAY))

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