3

我将尝试尽可能简单地解释这一点。任何帮助将不胜感激,因为我不太擅长设计查询。

我有 3 个表:(我已经为这个问题减少了 users 表中不必要的列)

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.

任何有关我将如何做到这一点的帮助将不胜感激!

4

2 回答 2

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

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-03-19T08:10:39.543 回答
1

这可以帮助您构建您的请求:

MySql 连接三个表

http://www.techotopia.com/index.php/Joining_Tables_in_MySQL

于 2013-03-19T08:13:58.663 回答