I have 4 tables that I need to join in a single query where the result is:
nome |COUNT(terrenosDeCadaID)|COUNT(conversasDeCadaID)
joao | 2 | 3
maria| 1 | 2
The tables look like this:
users
id|nome
1 |joao
2 |maria
terrenos
id|idterreno |iduser(mesmo que id da tabela users)
1 | 20 | 1
2 | 21 | 1
3 | 22 | 2
conversas
id|idterreno|msg|iduser(mesmo que id da tabela users)
1 | 20 | a | 1
2 | 20 | b | 1
3 | 21 | c | 1
4 | 22 | d | 2
5 | 22 | d | 2
I tried to do with INNER JOIN, but I'm still a beginner, and I could not, when I added the conversations went wrong, below what I tried:
SELECT nome, COUNT(t.iduser), COUNT(c.user) FROM users u
INNER JOIN terrenos t on u.id = t.iduser
INNER JOIN conversas c on u.id =t.iduser
GROUP BY t.iduser