左连接的SQL计数结果

时间:2013-05-02 22:03:21

标签: sql count group-by

我正在尝试从左连接中获取一个表的总数,其中有多个相同的id。这是我下面的例子 -

表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    |                |
+-------------+--------------+------+-----+---------+----------------+

表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       |                |
+------------+-------------+------+-----+---------+----------------+

到目前为止我有这个陈述 -

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

以下是结果示例:

+------------+-----------+
| 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 |

如何让account_id显示一次,将inv_count的总和显示为1行?所以我应该看 -

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

1 个答案:

答案 0 :(得分:4)

您只需将查询放在派生表中(并将其命名为tmp),然后按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 ;

为了进一步简化,您可以将内联子查询转换为LEFT连接。这样,不需要派生表。我还添加了别名并删除了ORDER BY。当你有ORDER BY时,MySQL会隐式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 ;