How to show the record that was most repeated in the MYSQL table?

Asked

Viewed 59 times

1

For example, if I made a record of John about 5 times ( ie, I made 5 records of John) and then made the record of other names normally,without repeating or anything, I wanted to know how to give a select showing John’s name, since it was the one that was most repeated, someone knows how?

1 answer

4


Try the following, Where Columnupled Name would be the column that has John 5x and Table Name would be your table

SELECT NomeColunaDuplicada, Count(*) as QtdRepeticoes FROM NomeTabela
GROUP BY NomeColunaDuplicada
HAVING Count(*) > 1
order by QtdRepeticoes desc;

This select will bring the repeated record and how many times it was repeated in equal descending order below: inserir a descrição da imagem aqui

Soon the one that was most repeated will be in first, but in case you want to return only it use:

SELECT NomeColunaDuplicada, Count(*) as QtdRepeticoes FROM NomeTabela
GROUP BY NomeColunaDuplicada
HAVING Count(*) > 1
order by QtdRepeticoes desc
limit 1;

Browser other questions tagged

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