【问题标题】:How to properly GROUP BY within a GROUP BY to get the COUNT of several COUNTS如何在 GROUP BY 中正确 GROUP BY 以获取多个 COUNTS 的 COUNT
【发布时间】:2020-02-24 21:26:31
【问题描述】:

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

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

【问题讨论】:

  • 怎么可能,你没有按account_id分组,而是在内select的select列表中使用?
  • @aschoerk 我完全想多了。我只需要将 GROUP BY 从 items.list_id 更改为 list.account_id...

标签: sql group-by count mariadb heidisql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2010-10-30
    • 2015-09-22
    相关资源
    最近更新 更多