新手试图解决MySQL查询逻辑问题

时间:2013-03-19 08:02:36

标签: php mysql sql join

我将尝试尽可能简单地解释这一点。任何帮助都会非常感激,因为我不太善于设计查询。

我有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.

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

2 个答案:

答案 0 :(得分: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

要进一步了解联接,请访问以下链接:

答案 1 :(得分:1)