Home How to get count by combining more than 2 tables in mysql
Reply: 1

How to get count by combining more than 2 tables in mysql

user3292629 Published in 2017-12-07 18:23:14Z

i have a table 'A' with status column, it can have 4 values. In table A i have table 'B's id, table B have table 'C's id. I want to get the status count FROM table 'A' by joining all these columns. The status column in table A is a foreign key from table 'D'. Table 'D' having status like 1-agreed, 2-not agreed etc

ATrimeloni Reply to 2017-12-07 18:33:04Z

The question is missing some information that might be helpful. Particularly, what exactly you are wanting to count. (i.e. are you just trying to count ALL rows, or are you trying to count the number of rows in table A that have each status). I'll put together an answer that assumes that latter.

I'll also just assume that "id" is the primary key of its own table, and that id will be the id from other tables inside a table.

select A.statusField, count(*)
from A
join B on (A.Bid = B.id)
join C on (B.Cid = C.id)
group by A.statusField

Hope that helps.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.277047 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO