0

I have a user table and user_friend table. user_friend shows who I am friend with (so it's uid and fid). I want to get the info of all my friends, so I did:

SELECT UID, Name FROM user WHERE UID = (SELECT FID FROM user_friend WHERE UID = 567445724);

but it gives me a Subquery returns more than 1 row, how should I modify this query?

0

5 Answers 5

3

You should try JOIN like

select u.uid,u.name 
   from user u join user_freinds uf on u.uid=uf.fid 
        where u.uid=567445724
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT UID, Name FROM user
WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);`

Alternatively, in MySQL, I believe I recall reading that joins are often much fasdter than subselects. So perhaps:

SELECT user.UID, user.Name FROM user, user_friend WHERE user.UID = user_friend.FID AND user_friend.UID = 567445724;

1 Comment

wonder why this only returns one entries while actually the subquery returns 2... and yes the fid is in the uid
0

You have to use the in keyword.

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);

Comments

0

Use IN instead of equals:

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);

Comments

0
select user.uid, user.name from user, user_friends 
where user.uid=user_friends.fid 
  and user.uid=567445724

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.