Mysql join from multiple tables

后端 未结 4 1978
半阙折子戏
半阙折子戏 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:06

    As I understand it, you want to find the name and posts of all your friends, not any friend that's in the friend table at all...?

    Your own user id being in $myId, this should do it (newest posts first);

    EDIT: Added status check for friends

    SELECT m.f_name, m.l_name, p.`text`
    FROM members m
    JOIN posts p 
      ON m.member_id = p.user_id
    JOIN friends f 
      ON f.`to` = m.member_id OR f.`from` = m.member_id
    WHERE (f.`from` = $myId OR f.`to`= $myId)
      AND f.`status` = 1 AND m.member_id <> $myId
    ORDER BY p.p_id DESC
    

提交回复
热议问题