仅显示我和朋友的迷你博客

时间:2010-09-11 09:34:28

标签: php mysql twitter

我正在尝试制作一个MySQL查询,它将显示来自我和我朋友的迷你博客。就像Twitter一样。

这是我的表格;

--members
m_id INT
m_user VARCHAR
(rest are other member info)

--shouts
s_id INT
s_userid INT
s_content TEXT
s_posted DATETIME

--friends
f_userid INT
f_friendid INT
f_status INT // 0=pending, 1=approved
f_create DATETIME
f_update DATETIME

注意:友谊有2个条目

例如:f_userid = ME,f_friendid = MYFRIEND,反之亦然

f_userid = MYFRIEND,f_friendid = ME

有效但不使用friendlist表的代码是;

SELECT * FROM shouts 
JOIN members 
ON members.m_id = shouts.s_uid 
ORDER BY s_posted DESC

在添加好友表信息时遇到问题。

我相信我只缺少一行代码或2 ...

我试过这个,但失败了。

SELECT * FROM shouts 
JOIN members 
ON members.m_id = shouts.s_uid 
JOIN friends
ON friends.f_friendid = members.m_id
WHERE m_id = 1 //my id
AND f_userid = 1 //my id
AND f_status = 1
ORDER BY s_posted DESC

1 个答案:

答案 0 :(得分:0)

// GET LIST OF FRIEND ID's FIRST
    $sfflsql = mysql_query("SELECT f_friendid FROM friends 
                        WHERE f_userid = ".intval($_SESSION['lalala']['m_id'])." 
                        AND f_status = 1 
                        ORDER BY f_friendid");
    // CONVERT ARRAY INTO STRING EX: 1,2,3,4
    $sfflrs = "";
    while($s1 = mysql_fetch_assoc($sfflsql))
    {
        $sfflrs .= implode(",",$s1).",";
    }
    $sfflrs = substr($sfflrs,0,-1); // REMOVES LAST COMMA ,

    $feedsql = mysql_query("SELECT * FROM shouts 
                           INNER JOIN dbbbl_members 
                           ON members.m_id = shouts.s_uid 
                WHERE m_id IN (".$_SESSION['lalala']['m_id'].",".$sfflrs.")
                           ORDER BY s_posted DESC 
                           LIMIT 15");

它有效,但不确定这是否是正确的方法。