tags:

views:

20

answers:

1

I'm trying to make an MySQL query that will display mini-blogs from Me and My friends. Just like Twitter.

Here are my tables;

--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

Note: there are 2 entries for 1 friendship

ex: f_userid = ME, f_friendid = MYFRIEND and is vice-versa

f_userid = MYFRIEND, f_friendid = ME

The code that works, but doesn't use the friendlist table is;

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

having trouble adding in the friends table info.

I believe i'm just missing one line of code or 2...

I've tried this, but failed.

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
A: 
// 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");

It works, but not sure if it was the right way to do.

L0rry