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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:01:14+00:00 2026-06-07T12:01:14+00:00

I have a 4 column table, the primary key is a composite of id,

  • 0

I have a 4 column table, the primary key is a composite of id, gameId and flag.

id | gameId | score | flag
--------------------------
1  | 1      | 10    | 1
1  | 1      | 20    | 0
2  | 1      | 1     | 0
1  | 2      | 10    | 1
3  | 2      | 1     | 0

I need to update the table so that:

  • All of gameId 2 scores are added to gameId 1 where the id and flag is the same.
    (e.g. row 1 would have a score of 20 by adding the scores of row 1 and row 4)

  • If the above happens the gameId 2 row needs to be deleted.

  • Where the above isn’t found (e.g. there is a row where the gameId is 2 but the id and flag don’t match another row), the gameId can just be changed to 1.

So my table after the SQL is complete should look like this:

id | gameId | score | flag
--------------------------
1  | 1      | 20    | 1
1  | 1      | 20    | 0
2  | 1      | 1     | 0
3  | 1      | 1     | 0

How can I write this in SQL?
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-07T12:01:15+00:00Added an answer on June 7, 2026 at 12:01 pm

    think this going to work:

    Try 1

    UPDATE score_list AS t2
    LEFT JOIN score_list AS t1 ON
      (t1.id = t2.id AND t1.flag = t2.flag AND t1.gameId = 1)
    SET
      t1.score = t1.score + t2.score,
      t2.gameId = IF(t1.gameId IS NULL, 1, t2.gameId)
    WHERE t2.gameId = 2;
    
    DELETE FROM score_list WHERE gameId = 2;
    

    Try 2

    # add scores to gameId = 1
    UPDATE score_list AS t2
    LEFT JOIN score_list AS t1 ON
      (t1.id = t2.id AND t1.flag = t2.flag AND t1.gameId = 1)
    SET
      t1.score = t1.score + t2.score
    WHERE t2.gameId = 2;
    
    # try to move gameID 2 to gameId 1, using ignore to allow rows to fail, as gameId alredy exists
    UPDATE IGNORE score_list SET gameId = 1 WHERE gameId = 2;
    
    # delete all not updated rows from last query
    DELETE FROM score_list WHERE gameId = 2;
    
    • 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 contains an Id column as a primary key. There
I have a table whose primary key is a column named St_ID . I
I have a two-column primary key on a table. I have attempted to alter
So I have a table with an identity column as the primary key, so
I have a N to N table with columns: Id (primary key) (I need
I have a table that has a composite key that consists of two int
I have a table with a IDENTITY Column as Primary Key (a classic ID
I have a table with a primary key that is a varchar(255). Some cases
I have a JPA Entity StatsEntity which has a composite primary key that is
I have a table A with a composite primary key and I have table

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.