首页 > 解决方案 > 如何在 GROUP BY 中正确 GROUP BY 以获取多个 COUNTS 的 COUNT

问题描述

所以我需要从不同的用户帐户中获取数量项目的计数。基本上,项目存储在列表中,然后存储在帐户中。问题是这些项目有一个 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 个计数。

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

标签: sqlgroup-bycountmariadbheidisql

解决方案


就问题而言,您当前的尝试几乎就在那里。您只需要更改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

推荐阅读