2013-03-19 46 views
3

我將盡可能簡單地解釋這一點。任何幫助將不勝感激,因爲我不擅長設計查詢。嘗試解決MySQL查詢邏輯問題的新手

我有3個表:(我已削減了不必要的列在用戶表這個問題)

USERS: 
id, username, password 

PENDINGREVIEW: 
id, articleid, requestid, content, writerid, datewritten 

ARTICLES: 
id, title, length, requestedby, content, writtenby, daterequested, datecompleted 

所以說我有review.php。在review.php我希望它顯示特定用戶的pendingreview表中的所有文章。因此,像這樣:

SELECT * FROM pendingreview WHERE requestid = $userid (where $userid = the id of the user) 

什麼其實我是想顯示在review.php雖然是從articles表的文章,多少文章都在pendingreview表對應於每個這些文章的列表。該列表只能由用戶請求的文章組成。

所以我想review.php看起來像這樣:

  • Your "How to build a bike" article request has 3 articles pending review.
  • Your "How to mow the lawn" article request has 12 articles pending review.

上我會怎麼做任何幫助,將不勝感激!

+0

我沒有任何問號。請先嚐試一下。並閱讀[faq] – 2013-03-19 08:04:01

+0

我將使用什麼查詢?我不知道從哪裏開始! – KriiV 2013-03-19 08:07:16

+0

@KriiV你的'SELECT ...'的例子正朝着一個好的方向前進。你需要'JOIN'你的表:[mysql-manual](http://dev.mysql.com/doc/refman/5.1/en/join.html),並在你的文章的ID上使用'count' ... – michi 2013-03-19 08:12:20

回答

3
SELECT a.title, COUNT(*) TotalPending 
FROM Articles a 
     INNER JOIN PendingReview b 
      ON a.ID = b.articleID 
WHERE b.RequestID = 'ID HERE' 
GROUP BY a.title 

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

+0

#1064 - 您的SQL語法中有錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在'a.ID = b.articleID WHERE b.RequestID ='3'GROUP BY a.title LIMIT 0,30'在第4行附近使用正確的語法 – KriiV 2013-03-19 08:12:42

+1

將'ON ''a.id'之前。老兄認真閱讀文檔。 – itachi 2013-03-19 08:15:20

+0

@KriiV更新。 – 2013-03-19 08:17:57