Sachin Vairagi Sachin Vairagi - 1 year ago 76
MySQL Question

mysql get friends from single table

I have a single table for friend request with status as pending , accepted or rejected, how to get my friends from this table ?
enter image description here

I followed this one but it is returning my own id i.e. 43

select distinct
case requester_user_id
when @myid then requested_user_id
else requester_user_id
end id
from friends
where @myid in (requester_user_id, requested_user_id)

I want to get user id 42 and 41 , please help!!! Thanks much.

Answer Source

Use IF condition in select statement to find friend_user_id.

IF(@myid = requestFromUserId,requestToUserId,requestFromUserId) as friend_user_id 
from friends 
where (requestFromUserId = @myid or requestToUserId = @myid) and status = 'accepted'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download