Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:45:47+00:00 2026-06-11T17:45:47+00:00

I have a table with fields id, votes(for each users), rating. Task: Counting user

  • 0

I have a table with fields id, votes(for each users), rating.

Task: Counting user rating based on votes for him and for others. that is, each time i update the field votes needed recalculation field rating.

Which means some can be on the 3rd place. voted for him and that he would be stood up to 2rd place, and the other vice versa – from 2 to 3. (in rating fiels)

How to solve this problem? Each time update the field to count users ratings on php and do a lot of update query in mysql is very expensive.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:45:48+00:00Added an answer on June 11, 2026 at 5:45 pm

    If you want to get the ratings with a select without having a rating column, then this is the way. However from a performance perspective I cannot guarantee this will be your best option. The way it works is that if two users have the same amount of votes they will have the same rating and then it will skip ahead the necessary number for the next different rating:

    set @rating:=0;
    set @count:=1;
    
    select id,
    case when @votes<>votes then @rating:=@rating+@count
    else @rating end as rating,
    case when @votes=votes then @count:=@count+1
    else @count:=1 end as count,
    @votes:=votes as votes
    from t1
    order by votes desc
    

    sqlfiddle

    This gives you an extra column which you can ignore, or you could wrap this select in to a subquery and have:

    select t2.id,t2.votes,t2.rating from (
    select id,
    case when @votes<>votes then @rating:=@rating+@count
    else @rating end as rating,
    case when @votes=votes then @count:=@count+1
    else @count:=1 end as count,
    @votes:=votes as votes
    from t1
    order by votes desc) as t2
    

    but the sqlfiddle is strangely giving inconsistent results so you’d have to do some testing. If anyone knows why this is I’d be interested in knowing the reason.

    If you want to get the rating for just one user then doing the subquery option and using a where after the from should give you the desired result. sqlfiddle – but again, inconsistent results, run it a few times and sometimes it gives rating as 10 other times as 30. I think testing in your db to see what happens will be best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that holds rows representing votes. The fields are an id
I have a table with fields user_id and votes . The votes field is
Have a task to make votes for photo. Because users can vote only one
I have a table rating with these fields rate_id, game_id, rating, ip . Let
I have a table with the following fields: last_update , votes & uptime .
i have payment table fields update reason and amount & total field are change
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have table A with fields user1 and user2 and table B with user3
I have a table the fields are Id1 , ID2 , Points1 , Points2
I have Access table with fields, in which there is some data and path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.