Perform two COUNT with INNER JOIN

Asked

Viewed 1,891 times

0

When using the SQL below to carry out the count of the number of brokers in a table using INNER JOIN, works perfectly.

SQL:

SELECT
    conta.acesso,
    count(corretor.cod) as num_corr
    FROM
    conta
    INNER JOIN corretor
    ON conta.id = corretor.cod
    where conta.id = '1015'

However, when trying to include the count also of the user number, then everything stops working. See the SQL down below:

SELECT
    conta.acesso,
    count(corretor.cod) as num_corr,
    count(usuarios.cliente) as num_user
    FROM
    conta
    INNER JOIN corretor
    ON conta.id = corretor.cod
    INNER JOIN usuarios
    ON conta.id = usuarios.cliente
    where conta.id = '1015'

In this SQL just included to count the users table and nothing else works.

  • Are you sure 'ON account.id = users.client' works? Try without Count first; i.e., SELECT account.access, Count(broker.Cod) as num_corr, users.client as num_user FROM...

  • 1

    @White Sim have done this test before and it works, only on the internet that does not

  • Want to try it with left Join? I’m just giving ideas...

  • 1

    @White as I do?

  • use group by?

  • What is your database? And your first SQL actually works without the Group by?

  • 1

    @Matheusribeiro My database is MYSQL and works without GROUP BY

  • Just replace, INNER JOIN with LEFT JOIN

Show 3 more comments

1 answer

1


Try these two ways, maybe it’ll help you or a light:

SELECT co.acesso
      ,c.num_corr
      ,u.num_user
  FROM conta co
 LEFT JOIN (select cod, count(cod) as num_corr from corretor)         c ON co.id = c.cod
 LEFT JOIN (select cliente, count(cliente) as num_user from usuarios) u ON co.id = u.cliente
 WHERE co.id = '1015'

OR

SELECT co.acesso
      ,count(c.cod)     as num_corr
      ,count(u.cliente) as num_user
  FROM conta co
 LEFT JOIN corretor c ON c.cod     = co.id
 LEFT JOIN usuarios u ON u.cliente = co.id 
 WHERE co.id = '1015'
 GROUP BY co.acesso

EDITED

SELECT co.acesso
      ,c.num_corr
      ,u.num_user
  FROM conta co
 LEFT JOIN (select cod, count(cod) as num_corr from corretor group by cod)             c ON co.id = c.cod
 LEFT JOIN (select cliente, count(cliente) as num_user from usuarios group by cliente) u ON co.id = u.cliente
 WHERE co.id = '1015'
 GROUP BY co.acesso
  • It worked in part because the result for the sum of the two Join are being equal. I will try to explain what is happening. values are repeating for joins results.

  • Both broker and users are displaying values 6. As in fact has in the database for broker 2 and user 3

  • The two ways I pointed you out gave the same result?

  • No, the first one doubled the value. The second one showed for broker as for user the value 6.

  • What will be the problem of these wrong results?

  • 1

    Check the edition of my reply, I made an example here similar to yours and it worked.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.