我不确定标题,任何人都可以编辑为高兴
表articles
+----------+-------------------------+----------+
| id | title | status |
+----------+-------------------------+----------+
| 1 | title 1 | accepted |
+----------+-------------------------+----------+
| 2 | title 2 | accepted |
+----------+-------------------------+----------+
| 3 | title 3 | rejected |
+----------+-------------------------+----------+
| 4 | title 4 | accepted |
+----------+---------+---------------+----------+
表article_reviewers
+----------+------------+-----------+
| id | article_id | user_id |
+----------+------------+-----------+
| 1 | 1 | 1 |
+----------+------------+-----------+
| 2 | 2 | 1 |
+----------+------------+-----------+
| 3 | 3 | 1 |
+----------+------------+-----------+
| 4 | 2 | 2 |
+----------+------------+-----------+
如何找到每个用户和每个状态的文章总数 从这张桌子。
从表中:
用户id 1
,有2篇被接受的文章和1篇被拒绝的文章
用户id 2
,有1篇被接受的文章和0篇被拒绝的文章。
如果我做了类似的事情:
SELECT count(*) total FROM article_reviewers ar
INNER JOIN articles a
ON ar.article_id = a.id
GROUP BY ar.user_id
我得到了每个用户的全文,我如何找到每个状态?
有人能告诉我如何做到这一点,我很无能为力。
答案 0 :(得分:2)
你快到了:
SELECT ar.user_id, a.status, count(*) total
FROM article_reviewers ar
INNER JOIN articles a
ON ar.article_id = a.id
GROUP BY ar.user_id, a.status