Select with users who most sent messages

-1

I'm making a system and I have to know which user sent the most messages. My tables look something 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:)

    
asked by anonymous 12.08.2017 / 21:56

1 answer

1

Just use 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

Function documentation:   link

And if you just want the one who sent you the most, you can limit the results to just one line, using 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: link

    
12.08.2017 / 22:51