使用用户朋友的帖子获取用户的帖子

时间:2017-04-12 13:15:47

标签: php mysql

我想从posts表格中获取当前用户朋友的帖子,我试过的是这段代码:

SELECT 
    `p`.id,
    `p`.thetext,
    `p`.time,
    `p`.location
FROM 
    `friends` as f,
    `users` as u,
    `posts` as p
WHERE 
    `f`.frid = `u`.id
AND 
    `p`.publisher = `u`.username
AND 
    `f`.username = '$userusername'
ORDER BY 
    `p`.orderdate DESC

此代码正在收到用户的朋友帖子,但我想显示用户的帖子和用户朋友的帖子(有订单)。

该怎么办?

2 个答案:

答案 0 :(得分:0)

SELECT 
    `p`.id,
    `p`.thetext,
    `p`.time,
    `p`.location
FROM `posts` as p
INNER JOIN  `users` as u ON `p`.publisher = `u`.username
LEFT JOIN `friends` as f ON `f`.frid = `u`.id
WHERE 
    `f`.username = '$userusername'
ORDER BY 
    `p`.orderdate DESC

请尝试以上代码。 希望这会有所帮助。

答案 1 :(得分:0)

SELECT 
    `p`.id,
    `p`.thetext,
    `p`.time,
    `p`.location
FROM 
    `friends` as f,
    `users` as u,
    `posts` as p
WHERE 
    `f`.frid = `u`.id 
AND 
   ( `p`.publisher = `u`.username or `p`.publisher = '$userusername')
AND 
    `f`.username = '$userusername'
ORDER BY 
    `p`.orderdate DESC