I have tow table as below
FWTable
mid uname gender avatar email psw dob city
1 User 1 Male cat.jpg user1@gmail.com psw1 2015-05-18 Ahmedabad
2 User 2 Female Koala.jpg user2@gmail.com psw2 2015-05-15 Jamnagar
3 User 3 Male Desert.jpg user3@gmail.com psw3 2015-04-25 Porbandar
4 User 4 Female Jellyfish.jpg user4@gmail.com psw4 2015-06-11 Jamnagar
5 User 5 Male Penguins.jpg user5@gmail.com psw5 2015-07-10 Jamnagar
6 User 6 Female user6@gmail.com psw6 2015-05-12 Jamnagar
FriendsList
fid mid friend_id friend_status
1 1 2 1
2 1 3 0
3 2 4 0
1002 3 4 1
1003 5 1 1
当用户1(中间1)在线时,我会向那些朋友展示喜欢的。
2 User 2 Female Koala.jpg user2@gmail.com psw2 2015-05-15 Jamnagar
5 User 5 Male Penguins.jpg user5@gmail.com psw5 2015-07-10 Jamnagar
答案 0 :(得分:0)
with all_friendships as (
--making a distinct list of all friendships, for both directions
select mid as the_user, friend_id as the_friend
from FriendsList
where friend_status = 1
union
select friend_id as the_user, mid as the_friend
from FriendsList
where friend_status = 1
)
select mid, uname, gender, avatar, email, psw, dob, city
from all_friendships
inner join FWTable on the_friend = mid
where the_user = 1
看起来你想展示两个方向的友谊,不仅是1号中间的人是朋友,还有1号中间的朋友。这应该解决这个问题。
答案 1 :(得分:0)
试试这个:
select t1.*
from FWTable t1
join FriendsList t2 on t1.mid = t2.mid
where t2.friend_id = 1
and status = 1
union
select t1.*
from FWTable t1
join FriendsList t2 on t1.mid = t2.friend_id
where t2.mid = 1
and status = 1