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

  • Home
  • SEARCH
  • 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 7624103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:52:31+00:00 2026-05-31T04:52:31+00:00

It seems that the only way to use UPDATE for multiple rows is CASE

  • 0

It seems that the only way to use UPDATE for multiple rows is CASE as

UPDATE posts SET rates = CASE
WHEN post_id = '44' THEN rates + 'X'
WHEN post_id = '33' THEN rates + 'Y'
WHEN post_id = '73' THEN rates + 'Z'
WHEN post_id = '63' THEN rates + 'X'
...
ELSE rates END

Consider a table with 1 million rows. This needs to loop over 1 million records to update e.g. 10 rows.

If we use single UPDATE for each UPDATE as

UPDATE posts SET rates = rates + 'X' WHERE post_id='44'
UPDATE posts SET rates = rates + 'Y' WHERE post_id='33'
UPDATE posts SET rates = rates + 'Z' WHERE post_id='73'
UPDATE posts SET rates = rates + 'X' WHERE post_id='63'
...

we will have 10 queries, but we do not loop over 999,990 other rows.

Which is the most efficient methods when the number of UPDATEing rows is significantly lesser than the total rows in the table?

One More Question: When we have ELSE rates END in the CASE; how mysql skips this record? Is a heavy task comparable with write?

  • 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-05-31T04:52:33+00:00Added an answer on May 31, 2026 at 4:52 am

    Instead of asking which is most efficient, why did you not experiment using the available tools, such as EXPLAIN or profiling?
    Just add a where clause to your update –

    UPDATE posts SET rates = CASE post_id
        WHEN 44 THEN rates + 'X'
        WHEN 33 THEN rates + 'Y'
        WHEN 73 THEN rates + 'Z'
        WHEN 63 THEN rates + 'X'
        ...
        END
    WHERE post_id IN(44, 33, 73, 63...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I've read seems to indicate that the only way to do it is
it seems that on Rails or erb, the only way to output anything is
Is there any way to use Dapper.NET with stored procs that return multiple result
I have just inherited a server application, however it seems that the only copy
I have a weird issue that only seems to be affecting IE 7. The
I've been investigating this issue that only seems to get worse the more I
For Internet Explorer only it seems that the target (srcElement) of clicks, mousedowns, mouseups,
I only read a little bit about this topic, but it seems that the
I have an application that seems to throw exceptions only after the program has
I tried things like $_ENV['CLIENTNAME'] == 'Console' but that seems to work on only

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.