PHP MySQL查询JOIN HELP

时间:2011-05-20 08:08:52

标签: mysql join

嘿大家好所以,我正在为我的项目创建这种社交网络脚本,我需要它来显示类似于facebooks feed的新闻源,也就是说,显示你的朋友帖子和你自己的帖子...但是我的技术它只显示朋友的帖子。

我的查询如下:

SELECT 
                * 
            FROM 
                ajee_friends
            JOIN
                ajee_wall
            ON
                ajee_friends.fid = ajee_wall.uid
            WHERE 
                ajee_friends.uid = '$this->uid'

3 个答案:

答案 0 :(得分:5)

如果你想要你朋友的墙和你自己的墙,这可以做到(在我看来更可读作为subselect):

SELECT *
  FROM ajee_wall w
 WHERE w.uid IN (SELECT fid FROM ajee_friends WHERE uid = $this->uid)
    OR w.uid = $this->uid

答案 1 :(得分:2)

    SELECT *
    FROM ajee_wall w
    WHERE w.uid IN
        ( SELECT fid 
          FROM ajee_friends
          WHERE uid = $this->uid
        )
UNION ALL
    SELECT *
    FROM ajee_wall w
    WHERE w.uid = $this->uid

答案 2 :(得分:0)

我认为你可以使用Union all ... 像,

SELECT * FROM ajee_friends UNION ALL SELECT * FROM ajee_wall

它会显示你所有的帖子和你的朋友。你可以让它只显示几行限制。