1

I'm trying to get the total count of a table from a left join where there's a multiple of the same id. Here's my example below -

Table 1:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| project_id  | int(11)      | NO   |     | NULL    |                |
| token       | varchar(32)  | NO   |     | NULL    |                |
| email       | varchar(255) | NO   |     | NULL    |                |
| status      | char(1)      | NO   |     | 0       |                |
| permissions | varchar(255) | YES  |     | NULL    |                |
| created     | datetime     | NO   |     | NULL    |                |
| modified    | datetime     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

Table 2:

+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| name       | varchar(32) | NO   |     | NULL    |                |
| account_id | int(11)     | NO   |     | NULL    |                |
| created    | datetime    | NO   |     | NULL    |                |
| modified   | datetime    | NO   |     | NULL    |                |
| active     | tinyint(1)  | YES  |     | 1       |                |
+------------+-------------+------+-----+---------+----------------+

I have this statement so far -

SELECT account_id, (SELECT COUNT(invitations.id)
    FROM invitations WHERE invitations.project_id = projects.id) AS inv_count
FROM projects order by account_id;

And here's a sample of the results:

+------------+-----------+
| account_id | inv_count |
+------------+-----------+
|          1 |         0 |
|          2 |         2 |
|          2 |         0 |
|          3 |         4 |
|          3 |         0 |
|          3 |         4 |
|          3 |         0 |
|          4 |         6 |
|          4 |         3 |
|          4 |         3 |
|          4 |         5 |
|          4 |         3 |
|          4 |         9 |
|          5 |         6 |
|          5 |         0 |
|          5 |         4 |
|          5 |         2 |
|          5 |         2 |

How do I get account_id to show once and the sum of inv_count to show as 1 line? So I should see -

+------------+-----------+
| account_id | inv_count |
+------------+-----------+
|          1 |         0 |
|          2 |         2 |
|          3 |         8 |
4

1 回答 1

5

You only need to put your query in a derived table (and name it, say tmp) and then group by the account_id:

SELECT account_id,
       SUM(inv_count) AS inv_count
FROM
  ( SELECT account_id, 
           (SELECT COUNT(invitations.id)
            FROM invitations 
            WHERE invitations.project_id = projects.id
           ) AS inv_count
    FROM projects 
  ) AS tmp
GROUP BY account_id
ORDER BY account_id ;

To simplify it farther, you can convert the inline subquery to a LEFT join. This way, no derived table is needed. I've also added aliases and removed the ORDER BY. MySQL does an implicit ORDER BY when you have GROUP BY so it's not needed here (unless you want to order by some other expression, different from the one you group by):

SELECT 
    p.account_id,
    COUNT(i.id) AS inv_count 
FROM 
    projects AS p 
  LEFT JOIN 
    invitations AS i
      ON i.project_id = p.id
GROUP BY 
    p.account_id ;
于 2013-05-02T22:13:11.657 回答