See table row and "ranking"

Asked

Viewed 94 times

0

I’m creating a series of profiles, and I want to create a kind of ranking based on number of visits.

I’ve already created the script to add visits and everything is already right on the table users.

User_id|User_cover|User_name|User_content|User_pais|User_facebook|User_date|**User_views** 

The User_views sum to each view. Turns out I wanted to show the result of the query and calculate by column User_views to table position that this user is. Returning a number to display in HTML.

I can do it without script or will I have to adapt? And if I have to do this, what logic do I need to execute?

  • 1

    Friend, post what you already have, you want to save the data in a database probably, correct? Explain better what your question is.. or just want to visit the facebook site?

  • Look,this table above is a registration with user data,facebook is just a link for people to be friends with it. -Let’s assume that user x has 430 visits and another user y with 500 visits, I wanted through a query or something like show the position in the column "User_views" that adds the visits. So when the user access the profile x,appear in the ranking as 2nd or something of the type.Without using another table for this. Any suggestions?

  • I sort of got it, so... there are hundreds of ways to do something like this, there are many frameworks in PHP that help build, it’s kind of open to this idea of "how to do?" the more correct would be you come up with an idea at least a little already well formulated, and ask for advice

  • Just one more point, your question is on top of the database query itself, or is on the application part?

  • It is in the same query, sorry, you are right, it is very wide even. It is the query,I wanted something well optimized,the site is very robust.

1 answer

1

You can bring this column already from SQL. Just create a temporary column with auto increment and make an order by user_views. Follow an example in Mysql:

SELECT
  (@count := @count + 1) AS ranking,
  users.*
FROM users
  CROSS JOIN (SELECT @count := 0) AS dummy
ORDER BY users.user_views DESC;

Browser other questions tagged

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