Question

Select 1 
from Friend 
where Friend.UserID = 1 
  and FriendID = (select User.UserID from User where UserName = 'friend_user');

What I'm trying to do is to check whether a user is "friends" with another user. So when the user navigates to "foo.com/user/username" the username is passed and that's when I do the check. I would prefer to not use two selects but it seems that's the only way to do this. Any suggestions to the best way of doing a task like this would be appreciated.

Was it helpful?

Solution

You could use a JOIN instead:

Select 1 
from Friend f
join User u
    on f.FriendID = u.UserID
    and u.UserName = 'friend_user'
where 
    f.UserID = 1;

OTHER TIPS

Answer to: "This works, but what if I wanted to select stuff from the User table, like the Friends UserName and such? –"

Select u.username, count(f.friends)
from Friend f
join User u
    on f.FriendID = u.UserID
    and u.UserName = 'friend_user'
where 
    f.UserID = 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top