# count from two different tables possible?

**URL:** <https://forums.percona.com/t/count-from-two-different-tables-possible/863>\
**Category:** Other MySQL® Questions\
**Created:** [August 4, 2008, 8:08pm UTC](https://forums.percona.com/t/count-from-two-different-tables-possible/863 "2008-08-04T20:08:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jemminger](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jemminger](https://forums.percona.com/u/jemminger)\
**Post date:** [August 4, 2008, 8:08pm UTC](https://forums.percona.com/t/count-from-two-different-tables-possible/863/1 "2008-08-04T20:08:01Z")

</div>

is it possible to return the count of two different joins, e.g.

select name, count(posts.id) num\_posts, count(friends.id) num\_friendsfrom users inner join posts on posts.user\_id = users.id inner join friends on friends.user\_id = users.idgroup by users.id

when I try this, I get very large numbers for num\_posts and num\_friends, and the two numbers are identical. if I only try to select one of the counts, then i get the expected number.

thanks,  
jeff

---

<div class="post-metadata">

**Author:** ![teajay2](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@teajay2](https://forums.percona.com/u/teajay2)\
**Post date:** [August 4, 2008, 9:52pm UTC](https://forums.percona.com/t/count-from-two-different-tables-possible/863/2 "2008-08-04T21:52:53Z")

</div>

you might try to doing the counts prior to doing the joins.

for instance:

select nameleft join ( select count(1), user\_id from posts group by user\_id ) p on u.id = p.user\_idleft join ( select count(1), user\_id from friends group by user\_id ) f on u.id = f.user\_idfrom users u

---

<div class="post-metadata">

**Author:** ![jemminger](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jemminger](https://forums.percona.com/u/jemminger)\
**Post date:** [August 4, 2008, 10:06pm UTC](https://forums.percona.com/t/count-from-two-different-tables-possible/863/3 "2008-08-04T22:06:32Z")

</div>

fantastic… looks like that does it. thanks!
