2012-12-15 132 views
0

我想創建一個新聞更新系統,使用MySQL和PHP。但我不能讓它工作,具有聯接保護 - 諮詢熱點和PHP麻煩,所以我可以顯示連接的新聞更新和所有的意見(如果一個有效的用戶登錄,人可以單獨刪除每個註釋)。但這只是我遇到麻煩的JOIN語句。使用註釋創建新聞更新,如何連接表?

我有這些表和DB模式

  1. news_tbl(news_id,日期,用戶(FK到users_tbl.username),標題,bodyText的和圖片)
  2. users_tbl(用戶名,電子郵件,密碼,用戶類型)
  3. comments_tbl(comments_id,名稱,註釋,news_id(FK到news_id))

我已經試過這一點:

$sqlquery ="SELECT news_tbl.*, users_tbl.*, comments_tbl.*, 
COUNT(comments_tbl.comments_id) AS comments_count 
FROM news_tbl 
LEFT JOIN users_tbl 
ON news_tbl.user = users_tbl.username 
LEFT JOIN comments_tbl 
ON comments_tbl.news_id = news_tbl.news_id 
GROUP BY news_tbl.news_id "; 

但後來我只能顯示一個評論,我想所有的意見,我想fecth的每個評論的ID,所以用戶可以單獨刪除每個註釋。而且,如果評論未被寫入,我也無法獲得新聞ID。

+2

歡迎SO! [你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – weltschmerz

+0

是..請分享您的查詢.. IES ... – ethrbunny

+1

@Charles .. * doppelganger!*我從你的評論開始花了大約三十秒,想知道爲什麼我發佈了這個,同時也沒有修復OP的愚蠢格式。 – Charles

回答

0

這應該得到大概你想要的東西,但你應該總是分享您的查詢,還可以指定正是你假裝達到的目標。

Select ut.user_id,nt.news_id,nt.headline, nt.body,ct.comments_id, 
ct.comment,count(ct.comments_id) as total 
from news_tbl nt 
inner join users_tbl ut on ut.user_id=nt.user_id 
left join comments_tbl ct on ct.news_id=nt.news_id 
group by ut.user_id,nt.news_id 

祝你好運。

+0

不能仍然得到它的工作:( – user1906437

+0

@ user1906437嘗試'左加入comments_tbl ct在ct.news_id = nt.news_id'而不是。 – Ivo

+0

@ user1906437只是編輯我的答案 – Ivo