Repeat id query most times

Asked

Viewed 51 times

1

I have two tables, one user and another visitas. On the table visitas i save the ID of the user that was visited and who visited.

How to make a query where I leave users in order of more number of visits? I do not keep the total anywhere

Any hint?

  • 1

    I don’t know how the structure is but I believe that a count() with a group by resolve.

  • Edit the question to include important details. For example: what is a visit? does a user visit another? Do you have one line per visit for each user? If you provide the ER model of the tables, maybe someone can respond directly with the SQL expression that can help you. Otherwise, many interactions of comments are required.

1 answer

3


Just a select with a totalizing function

select userid, count(*)
from visitas
group by userid
order by 2 desc

To order use a order by, so the data appears with the most visited users first.

Browser other questions tagged

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