20

如何从带有分组的内部连接中选择计数?

SELECT COUNT(table1.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act
GROUP BY table1.act

这将返回在 table2 中找到的动作计数。

添加

 SELECT table1.act, COUNT(table1.act) AS test

退货

act     test
------- ----
17682   3
17679   3
17677   3
11636   1
11505   1

我想收到发现的行为总数。

所以我想得到5。你能帮忙吗?

4

5 回答 5

31

您可以将该查询包装到另一个查询中:

SELECT COUNT(*) FROM (
    SELECT COUNT(table1.act) AS actCount FROM table1
    INNER JOIN table2 ON table1.act = table2.act
    GROUP BY table1.act
) t
于 2012-05-06T23:45:42.627 回答
7

使用不同的计数

SELECT COUNT(distinct table1.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act
于 2012-05-06T23:48:45.813 回答
1
SELECT COUNT(table2.act) FROM table1
INNER JOIN table2 ON table1.act = table2.act
于 2012-05-06T23:45:54.263 回答
1

如果你只想要结果计数,

SELECT     COUNT(1)
FROM       table1
INNER JOIN table2 ON table1.act = table2.act
GROUP BY   table1.act

那应该给你

于 2012-05-06T23:47:06.277 回答
0

用 SELECT COUNT 语句包装整个查询,如 Mosty Mostacho 的答案所示,只是想用as语句发布这个答案,因为通常最好返回带有列标题的结果。

SELECT COUNT(*) as FilteredActs FROM (
    SELECT COUNT(table1.act) AS actCount FROM table1
    INNER JOIN table2 ON table1.act = table2.act
    GROUP BY table1.act
) T

结果:

输出结果

于 2021-10-27T01:14:41.387 回答