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 8808575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:39:34+00:00 2026-06-14T02:39:34+00:00

I have an users table where I store user info along with a reputation

  • 0

I have an users table where I store user info along with a reputation score like Stackoverflow has.

My question is how to update multiple users’ reputations with a single MySQL query.

Here’s my users table

user_id  reputation
  1          11
  2         202
  3        3003
  4         444
  5         555

Let’s say user_id=1 downvotes user_id=4 and user_id=5. In this scenario user_id=1’s reputation loses 2 points and user_id=4,5 both lose 10 points.

The users table would then look like this:

user_id  reputation
  1           9 //lost 2 points
  2         202
  3        3003
  4         434 //lost 10 points
  5         545 //lost 10 points

I know the MySQL query shown below works but I’m wondering a generalizable solution in the case that the number of users to UPDATE is variable and depends on the voting.

UPDATE `users`
 SET `reputation` = CASE
 WHEN `user_id` = '1' THEN reputation - 2
 WHEN `user_id` = '4' THEN reputation - 10
 WHEN `user_id` = '5' THEN reputation - 10
 ELSE `reputation` END
  • 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-14T02:39:35+00:00Added an answer on June 14, 2026 at 2:39 am

    You can replace the second WHEN with an IN() clause. It still requires some dynamic query building in your application code, but if your API allows direct binding of arrays into an IN() clause, this is much easier.

    UPDATE `users`
     SET `reputation` = CASE
     WHEN `user_id` = '1' THEN reputation - 2
     WHEN `user_id` IN ('2','3','4') THEN reputation - 10
     ELSE `reputation` END
    

    I will admit I find it a bit awkward to force all of this into one query though. Unless performance is a real issue, I would rather see this as two queries wrapped in a transaction – one to subtract -2 from user_id=1 and one to subtract the -10 users.

    /* Only on InnoDB tables... */
    START TRANSACTION;
    Update `users` SET `reputation` = `reputation` - 2 WHERE `user_id` = 1;
    Update `users` SET `reputation` = `reputation` - 10 WHERE `user_id` IN (2,3,4);
    COMMIT;
    

    Documentation on MySQL transaction handling…

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

Sidebar

Related Questions

I have a table which stores test results like this: user | score |
Suppose you have a table like this: id user score It's a dump of
Let's say I have a table that has user_id, date, score, and every user
This is probably a newbie question. I have a table USER which contains info
I have a table that stores user information. The table has a userid (identity)
I have a table in which users store scores and other information about said
I have a table, called 'files', where I store file_id's. Basically, users on my
I have four JTables which for the user looks like one table. (Why four
I have a table joining two models in a has many through relationship. Users
I have a stored procedure which accepts a User defined table type called SeasonTable

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.