i have two tables
one for users which keeps the user info
another is for friends
users_table : id , username , avatar
friends_table : id , u1_id , u2_id
now i want to show each user friends in his profile
and i want to do it with one query
so in friends table my user could be in the u1_id or u2_id based on who has requested the friendship
and becuz i want to join each friend to the users_table and get his username and avatar i cant simply do
select * from friends_table where u1_id = $profile_id || u2_id = $profile_id
so i have to run two query
select f.u1_id , u.username , u.avatar from friends_table f join users_table u on f.u1_id = u.id
where f.u2_id = $profile_id
select f.u2_id , u.username , u.avatar from friends_table f join users_table u on f.u2_id = u.id
where f.u1_id = $profile_id
is there any way to do this with one query ?
or something like this
select f.u1_id , f.u2_id , u.username , u.avatar from friends_table where u1_id = $profile_id || u2_id = $profile_id
if( u1_id = $profile_id )
join users_table u on f.u2_id = u.id
if( u2_id = $profile_id )
join users_table u on f.u1_id = u.id
my problem is in the join part , i want to join the friends_table with usres_table on the friends id not current owner of profile id
1 Answer