Mysql join from multiple tables

后端 未结 4 1976
半阙折子戏
半阙折子戏 2021-01-16 16:43

I have 3 tables

friends
posts
members

friends
========
id, to, from (user_id\'s), status

there are 3 status\'s -1 = denied, 0 = no response/new, 1 = accept         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 17:03

    I added a subquery to get all your friends since I assumed that if you have these records

    Friends
    ==================================
    ID      TO       FROM     STATUS
    ==================================
    1       1        2         1
    2       3        1         1
    

    and your member_id = 1, your friends are 2, 3. right?

    SELECT  b.f_name,
            b.L_name,
            c.`text`
    FROM
        (
            SELECT `to` friendID
            FROM    friends
            WHERE   `status` = 1 AND
                    `from` = ?             -- change this to your ID
            UNION
            SELECT `from` friendID
            FROM    friends
            WHERE   `status` = 1 AND
                    `to` = ?               -- change this to your ID
        ) a  INNER JOIN members b
                ON a.friendID = b.member_ID
            LEFT JOIN   posts c
                ON a.friendID = c.user_id
    

提交回复
热议问题