how to get friends who like my topic
i have users table has id field and the friendship table frienduser has friend1id or friend2id according who sent a request to whom first and likes table 开发者_C百科| thread_id - userid i want select from users the persons who are my friends and liked the topic
The ugly part of this is the fact that the frienduser table could link on either ID. I think the simplest way around this would be with a union.
/* Case 1: My ID is friend1id, my friend is friend2id */
select u2.*
from users u1
inner join frienduser f
on u1.id = f.friend1id
inner join users u2
on f.friend2id = u2.id
inner join likes l
on u2.id = l.userid
where u1.id = @MyUserId
and l.threadid = @MyThreadId
union
/* Case 2: My ID is friend2id, my friend is friend1id */
select u2.*
from users u1
inner join frienduser f
on u1.id = f.friend2id
inner join users u2
on f.friend1id = u2.id
inner join likes l
on u2.id = l.userid
where u1.id = @MyUserId
and l.threadid = @MyThreadId
There you go.
精彩评论