简体   繁体   中英

Get Total Rows From Join Based On Value From Join Table

Im not sure about the title, anyone can edit as pleased

Table articles

+----------+-------------------------+----------+
|   id     |  title                  | status   |
+----------+-------------------------+----------+
|    1     |  title 1                | accepted |
+----------+-------------------------+----------+
|    2     |  title 2                | accepted |
+----------+-------------------------+----------+
|    3     |  title 3                | rejected |
+----------+-------------------------+----------+
|    4     |  title 4                | accepted |
+----------+---------+---------------+----------+

Table article_reviewers

+----------+------------+-----------+
|   id     | article_id |  user_id  |
+----------+------------+-----------+
|    1     |     1      |     1     |
+----------+------------+-----------+
|    2     |     2      |     1     |
+----------+------------+-----------+
|    3     |     3      |     1     |
+----------+------------+-----------+
|    4     |     2      |     2     |
+----------+------------+-----------+

How can i find total of article for each user and for each status from this table.

From table:
user with id 1 , have 2 accepted article and 1 rejected article.
user with id 2 , have 1 accepted article and 0 rejected article.

If i make something like:

SELECT count(*) total FROM article_reviewers ar
INNER JOIN articles a
ON ar.article_id = a.id
GROUP BY ar.user_id

I got total article for each user, how can i find for each status?
Can someone show me light on how to do this, im quite clueless.

You're nearly there:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM