2017-04-12 100 views
-2

我想從該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 

此代碼獲取用戶的朋友的帖子,但我想顯示的用戶的帖子和用戶的朋友的帖子(與排序)。

請問該做什麼?

+0

好吧......看起來您需要了解數據庫模式和SQL。當事情相對簡單且易於理解時,您應該現在就做到這一點。請不要在SO上提出這個問題。 –

+0

謝謝你的建議,但你能回答我的問題嗎? –

+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 

請嘗試上面的代碼。 希望這會有所幫助。

+0

這個查詢並沒有給我帶來什麼 –

+0

你確定每個發表帖子的用戶都有'posts'和'user'表中的條目,''p'.publisher ='u'.username'會一直返回true。 –

+0

不,也許用戶還沒有發佈帖子呢!這意味着用戶可能沒有文章 –

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

它的工作,但有重複的列! –

+1

不同的帖子id然後再試一次'SELECT distinct('p'.id), 'p'.thetext, 'p'.time, 'p'.location FROM' –