4
I have 3 tables in MYSQL.
Bill:
id nome
1 caio
2 zé
3 marcelo
Followers (the account id, that is, the user has 2 followers):
idqual
1
1
2
2
2
3
Products (the account id, that is, the user caio has 3 products):
idqual
1
1
1
2
2
3
What I have to do is a query sorting by the number of products that each user has and by the number of followers that each user has, then I have to list how many products and how many followers each user has, I thought it would be easy to do this, and I did so:
SELECT conta.id, conta.nome, count(produtos.idqual) as somaprodutos, count(seguidores.idqual) as somaseguidores FROM conta
LEFT JOIN produtos ON conta.id = produtos.idqual
LEFT JOIN seguidores ON conta.id = seguidores.idqual
GROUP BY conta.id, conta.nome
ORDER BY SUM(somaprodutos) DESC,
SUM(somaseguidores) DESC
The error that occurs is that the value of the somafollowers and the domaproducts is the same, and this value is always the multiplication of them, example:
He is saying that the user Caio has 6 followers and 6 products (2 followers * 3 products), which I did wrong?
Explain to me the meaning of this division:
SUM(produtos.idqual/produtos.idqual)
?– Fellipe Soares
So, I had to add up how many times you go through each product, then I took the user id (idqual) and divided by himself, to always give 1, and then add up 1 + 1 + 1, to give the number of products that the user has. It was a certain gambit....
– caiocafardo
You can use the function
count()
for that reason. =)– Fellipe Soares
Opa, thanks, I did not know, but he continues multiplying the somaprodutos with the somafollowers....
– caiocafardo