Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Count from INNER JOIN

Tags:

sql

count

How can I select the count from an inner join with grouping?

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

This will return the count of act's found in table2.

Adding

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

Returns

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

I want to receive the count of act's found total.

So I want to get 5. Can you help?

like image 932
Steve Payne Avatar asked May 06 '12 23:05

Steve Payne


People also ask

How do I COUNT inner joins in SQL?

SQL SUM() and COUNT() with inner join The data from a subquery can be stored in a temporary table or alias. The data of these temporary tables can be used to manipulate data of another table. These two tables can be joined by themselves and to return a result. Here is a slide presentation of all aggregate functions.

How do I COUNT rows in joined table?

Show activity on this post. $query_string = ' SELECT groups. userGroupID, userGroup, count(users. userGroupID) AS howMany FROM groups_table AS groups JOIN users_table AS users ON users.

What does COUNT (*) do in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

How do I join two tables and counts in SQL?

To achieve this for multiple tables, use the UNION ALL. select sum(variableName. aliasName) from ( select count(*) as yourAliasName from yourTableName1 UNION ALL select count(*) as yourAliasName from yourTableName2 ) yourVariableName; Let us implement the above syntax.


1 Answers

You can wrap that query into another query:

SELECT COUNT(*) FROM (
    SELECT COUNT(table1.act) AS actCount FROM table1
    INNER JOIN table2 ON table1.act = table2.act
    GROUP BY table1.act
) t
like image 130
Mosty Mostacho Avatar answered Nov 09 '22 23:11

Mosty Mostacho