左连接的 SQL 计数结果

waf*_*les 1 sql group-by count

我正在尝试从具有多个相同 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    |                |
+-------------+--------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)

表 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       |                |
+------------+-------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)

到目前为止我有这个声明 -

SELECT account_id, (SELECT COUNT(invitations.id)
    FROM invitations WHERE invitations.project_id = projects.id) AS inv_count
FROM projects order by account_id;
Run Code Online (Sandbox Code Playgroud)

以下是结果示例:

+------------+-----------+
| 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 |
Run Code Online (Sandbox Code Playgroud)

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

+------------+-----------+
| account_id | inv_count |
+------------+-----------+
|          1 |         0 |
|          2 |         2 |
|          3 |         8 |
Run Code Online (Sandbox Code Playgroud)

ype*_*eᵀᴹ 5

您只需要将您的查询放在派生表中(并命名它,例如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 ;
Run Code Online (Sandbox Code Playgroud)

为了进一步简化,您可以将内联子查询转换为LEFT连接。这样,就不需要派生表。我还添加了别名并删除了ORDER BY. MySQL 会ORDER BY在您拥有时执行隐式操作,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 ;
Run Code Online (Sandbox Code Playgroud)