2013-03-19 50 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