문제

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.

도움이 되었습니까?

해결책

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;

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top