开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜