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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:25:10+00:00 2026-06-18T19:25:10+00:00

Good day, I have a MySQL table which has some duplicate rows that have

  • 0

Good day,

I have a MySQL table which has some duplicate rows that have to be removed while adding a value from one column in the duplicated rows to the original.

The problem was caused when another column had the wrong values and that is now fixed but it left the balances split among different rows which have to be added together. The newer rows that were added must then be removed.

In this example, the userid column determines if they are duplicates (or triplicates). userid 6 is duplicated and userid 3 is triplicated.

As an example for userid 3 it has to add up all balances from rows 3, 11 and 13 and has to put that total into row 3 and then remove rows 11 and 13. The balance columns of both of those have to be added together into the original, lower ID row and the newer, higher ID rows must be removed.

ID | balance | userid
---------------------
1  | 10      | 1
2  | 15      | 2
3  | 300     | 3
4  | 80      | 4
5  | 0       | 5
6  | 65      | 6
7  | 178     | 7
8  | 201     | 8
9  | 92      | 9
10 | 0       | 10
11 | 140     | 3
12 | 46      | 6
13 | 30      | 3

I hope that is clear enough and that I have provided enough info. Thanks =)

  • 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-18T19:25:11+00:00Added an answer on June 18, 2026 at 7:25 pm

    Two steps.

    1. Update:

        UPDATE 
            tableX AS t
          JOIN
            ( SELECT userid
                   , MIN(id) AS min_id
                   , SUM(balance) AS sum_balance
              FROM tableX
              GROUP BY userid
            ) AS c
            ON  t.userid = c.userid
        SET
            t.balance = CASE WHEN t.id = c.min_id 
                            THEN c.sum_balance
                            ELSE 0 
                        END ;
    

    2. Remove the extra rows:

        DELETE t
        FROM
            tableX AS t
          JOIN
            ( SELECT userid
                   , MIN(id) AS min_id
              FROM tableX
              GROUP BY userid
            ) AS c
            ON  t.userid = c.userid
            AND t.id > c.min_id
        WHERE
            t.balance = 0 ;
    

    Once you have this solved, it would be good to add a UNIQUE constraint on userid as it seems you want to be storing the balance for each user here. That will avoid any duplicates in the future. You could also remove the (useless?) id column.

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

Sidebar

Related Questions

Good day I have a custom TextBox that has a IndicatorTextBox.ui.xml file as well
Good day, I have a practical mysql question to which I can't find help
Good day - several days have passed while i was trying to transport my
Good day, I have a class that implements the LoaderCallbacks, and hence have the
Good Day, I have a simple working routine in Perl that swaps two words:
Good Day, I have a Hibernate mapping that looks something like this: public class
Good day, I have created an object that will manage data access. My app
Good day, If I have for example the documents which have the following fields
Good day. I have a PHP & MySql background, and I'm just starting with
Good day! I want to switch from MySQL to CouchBase and I have been

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.