1

I'm having a bit of trouble figuring out an SQL statement that I need to make.

I have 2 tables

Friends

===================================
friendfromid | friendtoid | request
===================================

Users

=================
userid | username
=================

I need to get the userid and username of each user that is my friend.

My id is either in the friendfromid or friendtoid depending on if I am requested the user or the user requested me.

So basically what needs to happen is the script needs to look at the friends table, get all the rows where my id is either friendfromid or friendtoid check that the request field is set to 1, take all the rows that fit that match then get the ids and usernames of each friendfromid or friendtoid which isn't mine.

For instance, if my id was 8 and the friends id was 9, let's say they requested me their id would be in the friendfromid field and my id would be in the friendtoid field, that means the script would take their id (9) and match it to that user in the users table.

4 Answers 4

2

This will give friends list with friend id and friend name:

SELECT  u.userid , u.username
FROM Friends f 
JOIN Users u 
ON (( f.friendfromid ={the ID} AND friendtoid=u.userid)
OR ( f.friendtoid = {the ID}  AND friendfromid=u.userid))
WHERE f.request = 1

Put {the ID}= user id whose friend list required.

Sign up to request clarification or add additional context in comments.

4 Comments

I think you mean (friendfromid=? AND friendtoid=userid) OR (friendtoid=? AND friendfromid=userid).
What I mean is when fromid matches toid is the foreign key and vice-versa. Your entire join condition is equivalent to {the ID} = userid!
@SomnathMuluk: I had to remove the double equal signs, but yes it works perfect, thank you
sorry man....by mistake i had done double equal signs.... i have removed double equal signs and placed single equal answer now.
1

Should be something like this:

SELECT * 
FROM users 
JOIN friends 
ON friendfromid != userid
OR friendtoid != userid
WHERE request = 1
  AND userid = {the ID}

1 Comment

Incorrect. He wants to get the ID either from friendfromid or friendtoid where his user ID is on the opposite field.
1

Can't achieve in 1 query.

To get friendtoid with "my user ID" in friendfromid field:

SELECT b.friendtoid FROM Users a, Friends b WHERE request=1 AND friendfromid = a.userid

Vice versa for friendfromid.

1 Comment

Sorry, this doesnt make any sense .... I need to get the userid and and username from the users table
1

Use a UNION query, joining to friends as below

SELECT username FROM user INNER JOIN friends ON (userid = friendfrom) WHERE friendto = {myid) AND request = 1
UNION
SELECT username FROM user INNER JOIN friends ON (userid = friendto) WHERE friendfrom = {myid} AND request = 1

Remember you need indexes on the friendto and friendfrom columns for performance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.