2010-09-11 51 views
1

我正在嘗試做一個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個條目1個友誼

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

f_userid = MYFRIEND,f_friendid = ME

的代碼有效,但不使用好友名單表;

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 

回答

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"); 

它的工作原理,但不知道這是應該做的正確方法。