Jump to content

Join the same table to get/compare data (for friend list)


eevan79

Recommended Posts

I have USERS table and FRIENDS table. When user send friend request to other user data have been added into FRIENDS table. When other user accept request, data is also added (for his ID).

 

 

There's no problem to get approved (both data) friends, but I want to get pending list.

 

 

This is fields for FRIENDS table:

user_id | friend_id | friends

 

 

For example we have 2 users: one with ID 1, and other with ID 2. So user with ID 1 send friend request to ID 2 following data will be added into FRIENDS table:

1 | 2 | 1

 

 

When other user accept request:

2 | 1 | 1

 

 

So there is record for both users in FRIENDS table. So, how to get data only when there is one record (to get pending requests)?

 

 

This is query to get friend list:

 

SELECT z.friend_id, u.username, u.user_avatar
	  FROM 
                  " . FRIENDS_TABLE . " AS z, 
	  " . FRIENDS_TABLE . " AS m, 
                  " . USERS_TABLE . " AS u
				WHERE z.friend_id= m.user_id 
				AND m.friend_id= {$userID} 
				AND z.user_id = {$userID} 
				AND z.friend_id= u.user_id
				ORDER BY LOWER(u.username) DESC

Now I need query to get pending request (those that the user has not yet approved)?

[/size]

Link to comment
Share on other sites

Hi

 

Tend to agree with the above.

 

However I would think that you could left outer join friends with itself, where friend id on one is user id on the other AND vice versa. Then only return rows where there is no matching row.

 

Something like this:-

 

SELECT *
FROM friends a
LEFT OUTER JOIN friends b
ON a.user_id = b.friend_id
AND a.friend_id = b.user_id
WHERE b.user_id IS NULL

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.