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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:17:06+00:00 2026-06-09T04:17:06+00:00

I have a count table, and i would like to be able to sum

  • 0

I have a count table, and i would like to be able to sum the counts when renaming one of the compound key fields. This is difficult to explain, so let me show you some SQL:

Example table:

CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `type` int(11) NOT NULL,
  `count` int(11) NOT NULL,
  PRIMARY KEY (`id`,`type`)
);

Insert some data:

INSERT INTO test VALUES(1, 10, 1);
INSERT INTO test VALUES(2, 20, 3);
INSERT INTO test VALUES(2, 10, 3);
INSERT INTO test VALUES(2, 30, 3);

Query the data:

mysql> SELECT SUM(count) as count FROM test WHERE id = 2;
+-------+
| count |
+-------+
|     9 |
+-------+

mysql> SELECT SUM(count) as count FROM test WHERE type = 10;
+-------+
| count |
+-------+
|     4 |
+-------+

Works very well, is fast and flexible.

Now, I’d like to remap type 10 to type 20

UPDATE test SET type = 20 WHERE type = 10;
Duplicate entry '2-20' for key 'PRIMARY'

Using ON DUPLICATE KEY UPDATE here is invalid.
I figure it should be possible with a creative insert, but i’m not sure. Can anyone think of an approach ?

  • 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-09T04:17:07+00:00Added an answer on June 9, 2026 at 4:17 am

    One approach is to “loosen” the PRIMARY KEY, meaning, change it from a PRIMARY (unique) KEY to a non-unique KEY. This will allow for duplicate values, and will allow your UPDATE statement to succeed, such that you will have two (or more) rows with matching (id,type).
    (Note that this makes updating a single row problematic, so you would likely want to add a new column which can be unique. An AUTO_INCREMENT column would work nicely for that.)


    If you don’t want to do that, then the other approach would be to “combine” the counts for the type 10 and type 20 rows together (for each id) into a type 20 row, set the count for the type 10 rows to zero, and (optionally) remove the redundant type 10 rows in a separate statement.

    The statement below “combines” the type 10 and type 20 counts by “mapping” the 10 value to a 20 in the first select.


     -- combine count for types 10 and 20 as new count for type 20
     -- and set count to zero for type 10
    
     INSERT INTO test (id, `type`, `count`)
     SELECT t.id
          , IF(t.`type`=10,20,t.`type`) AS new_type
          , SUM(t.`count`) AS `count`
       FROM test t
      WHERE t.`type` IN (10,20)
      GROUP BY id, new_type
      UNION ALL
     SELECT u.id
          , u.`type`
          , 0 AS `count`
       FROM test u
      WHERE u.`type` = 10
         ON DUPLICATE KEY UPDATE `count` = VALUES(`count`);
    
     -- remove unnecessary type 10 rows.
     DELETE FROM test WHERE `type` = 10 AND `count` = 0;
    

    Note: The IF() expression in the first SELECT is equivalent to:

    CASE WHEN t.type = 10 THEN 20 ELSE t.type END
    

    The second select is getting us all the type 10 rows that will need to be updated. We concatenate those two result sets using the UNION ALL operator.

    Then we take the combined (concatenated) result set, and run them into an insert. Any place we hit a “duplicate” key, we perform the update action via the ON DUPLICATE KEY clause.

    You will likely want to do this in a transaction (if you are using InnoDB) and verify that the first statement completes successfully before executing a DELETE. If the first statement throws an exception (maybe there is a wonky trigger), then you would want to ROLLBACK the transaction. You’d also want to ROLLBACK if the DELETE fails for some reason (perhaps there is a foreign key constraint that would be violated.)

    Alternatively, you do not necessarily need to perform the DELETE, you could just leave the type 10 rows with counts of zero.


    IMPORTANT:

    Do NOT implement BOTH of these solutions! Only ONE of them.

    The first approach is a schema change, which doesn’t require any data changes. That change will allow your UPDATE to succeed.

    The second approach requires that the schema remain the same, and depends on the existence (and enforcement) of the UNIQUE KEY on (id,type).

    • 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 looks like this: id count 1 100 2 50
I have a table containing order details. I would like to be able to
For example, if we have a table Books, how would we count total number
i have a table which has many fields but i want to get count
Possible Duplicate: Count(*) vs Count(1) If I have a table, 'id' is primary key,
I have a list of values like this: (UXT8,U61J,U61W,U62U,X82U,U5NF,U635,U526,28FX) I would like to be
I have a table with around 15 columns. What I would like to be
I have SQL update query problem. I would like to be able to update
This is a complex one. But I have a table which has a DATETIME
I have some code like this: <h2 id=a>Header</h2> <table> <tr> <td>test</td> </tr> </table> <h2

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.