来自INNER JOIN的SQL计数

时间:2012-05-06 23:39:06

标签: sql count

如何通过分组从内部联接中选择计数?

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

这将返回表2中的行为计数。

添加

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

返回

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

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

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

4 个答案:

答案 0 :(得分:20)

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

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

答案 1 :(得分:3)

使用count distinct

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您只想要结果计数,

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

应该给你