如何在 GROUP BY 中正确 GROUP BY 以获得多个 COUNTS 的 COUNT

How to properly GROUP BY within a GROUP BY to get the COUNT of several COUNTS

所以我需要计算在不同用户帐户中找到的项目数量。基本上,项目存储在列表中,然后存储在帐户中。问题是项目有 list_id,但没有 account_id,即使列表有 account_id。如果项目也有一个 account_id,那会很容易,但它更像这样:

account -> list -> item

因此帐户和项目之间没有直接联系。我知道这不是最优的,但我不是设计数据库的人。我只需要使用它。

数据的结构如下:

account (Where the account_id comes from)
----------------------------------
| account_id | account_name| ... |
|------------|-------------|-----|
|          1 | account_1   | ... |
|          2 | account_2   | ... |
|          3 | account_3   | ... |
|          4 | account_4   | ... |
|          5 | account_5   | ... |
|        ... | ...         | ... |
---------------------------------- 

list (Where the list_id comes from. Also linked to the account table by the account_id FOREIGN KEY)
------------------------------------------
| list_id | list_name | account_id | ... |
|---------|-----------|------------| ... |
|       1 | list_1    |          1 | ... |
|       2 | list_2    |          1 | ... |
|       3 | list_3    |          2 | ... |
|       4 | list_4    |          2 | ... |
|       5 | list_5    |          3 | ... |
|     ... | ...       |        ... | ... |
------------------------------------------

item (Where the items come from. Also linked to the list table by the list_id FOREIGN KEY)
---------------------------------------
| item_id | item_name | list_id | ... |
|---------|-----------|---------|-----|
|       1 | item_1    |       1 | ... |
|       2 | item_2    |       1 | ... |
|       3 | item_3    |       2 | ... |
|       4 | item_4    |       2 | ... |
|       5 | item_5    |       3 | ... |
|     ... | ...       |     ... | ... |
---------------------------------------

What I manage to get (I've added the list_id even though it's uncessary to show you that there is an 
item_qty count for each list)
----------------------------------
| account_id | list_id| item_qty |
|------------|--------|----------|
|          1 |      1 |        6 |
|          1 |      2 |        1 |
|          1 |      3 |        5 |
|          2 |      4 |        2 |
|          2 |      5 |        2 |
---------------------------------- 

What the desired result is.
-------------------------------
| account_id | item_qty | ... |
|------------|----------|------
|          1 |       12 | ... |
|          2 |        4 | ... |
|        ... |      ... | ... |
-------------------------------

这是获取当前结果的代码:

SELECT list.account_id, COUNT(*) AS item_qty
FROM item
JOIN list ON list.list_id = item.list_id
GROUP BY items.list_id

以下是我为获得所需结果所做的努力:

SELECT * FROM (
SELECT list.account_id as id, COUNT(*) AS item_qty
FROM item
JOIN list ON list.list_id = item.list_id
GROUP BY items.list_id ) as t
GROUP BY id

但是,当我这样做时,我得到的只是 一个 列表中的计数。因此,如果帐户 1 中的列表 1 有 3 个项目,帐户 1 中的列表 2 有 4 个项目,即使我按帐户对列表进行分组,我的计数也是 3。

我需要的是获取列表中所有项目的计数,然后是帐户所有列表中所有项目的计数。

至于你目前的尝试,已经差不多了。您只需要更改 group by 子句以匹配 select 子句中的非聚合列:

SELECT list.account_id, COUNT(*) AS item_qty
FROM item
JOIN list ON list.list_id = item.list_id
GROUP BY list.account_id -- instead of items.list_id