Select with most messaging users

Asked

Viewed 51 times

-1

I’m making a system and I have to know which user sent the most messages. My tables are more or less like this:

User

id|nome|sobrenome|email| ...

Messages

id|uid|nome|email|assunto|msg| ...

I just need to know which user sent more messages,

From now on, thank you :)

  • There is the count() to return the number of lines, I think it would be enough.

  • Ta, but to return from the largest pro minor? Type, which user sent the most messages and which one sent the least ...

1 answer

1


Just use the count:

Select
u.id,
u.nome,
count(m.id) as qtd_mensagens
from mensagens m
inner join usuario u on u.id = m.uid
group by u.id, u.nome
order by qtd_mensagens desc

Job documentation: https://dev.mysql.com/doc/refman/5.7/en/counting-rows.html

and if you only want the most uploaded user, you can limit the results to just one line, using the limit

Select
u.id,
u.nome,
count(m.id) as qtd_mensagens
from mensagens m
inner join usuario u on u.id = m.uid
group by u.id, u.nome
order by qtd_mensagens desc
limit 1

Syntax documentation: https://dev.mysql.com/doc/refman/5.7/en/select.html

  • It worked out! Thank you very much

  • @Robertjunio don’t forget to mark as a response. Welcome to Stackoverflow pt and take the community tour: https://answall.com/tour

Browser other questions tagged

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