Home » excel » sql – Is there a simple MySQL query to add a bunch of values to specified rows?

sql – Is there a simple MySQL query to add a bunch of values to specified rows?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a MySQL table with several columns, including id and count. I just got an Excel spreadsheet with the same item ids paired with numbers that must be added to the count for each row.

For instance, here’s a subset of the data, showing only the relevant columns:

| id  | count |
|-----+-------|
| ... | ...   |
| 348 | 165   |
| 347 | 364   |
| 346 | 381   |
| 345 | 6017  |
| ... | ...   |

And this the data from the Excel spreadsheet:

| id  | add   |
|-----+-------|
| ... | ...   |
| 348 | 3,766 |
| 347 | 386   |
| 346 | 860   |
| 345 | 0     |
| ... | ...   |

I want to combine the count and add fields for each id so that the count is updated like so, leaving all other columns in the table untouched:

| id  | count |
|-----+-------|
| ... | ...   |
| 348 | 3931  |
| 347 | 750   |
| 346 | 1241  |
| 345 | 6017  |
| ... | ...   |

I know MySQL can do it, but I don’t know what the steps are. What format should I export the Excel data? CSV? TSV? How do I read the “adjustment” file in to MySQL? What is the syntax for this kind of UPDATE command?

How to&Answers:

You’ll need to do this in a couple of steps:

  1. CREATE a TEMPORARY TABLE.
  2. LOAD the data into the temporary table
  3. UPDATE the original table, using a JOIN against the temporary one.

CREATE [TEMPORARY] TABLE syntax is straightforward. It’s just like creating a table, but it goes away when your session ends. The table format should match the columns which you want to update. This can be accomplished simply using the CREATE ... (SELECT...) syntax, eg:

CREATE TEMPORARY TABLE `data_to_load` (
    SELECT
        `id`,
        `count`
    FROM
        `table_to_update`
    WHERE
        0
);

This will create a TEMPORARY TABLE called data_to_load, consisting of exactly the same format as the table_to_update columns id and count. The WHERE 0 will never match, so it just copies the format of the table, not the data.

Next, LOADing the data. The format isn’t picky, you just need to specify it. For standard CSV format as used by Excel, the command would be:

LOAD DATA
    INFILE 'exported_data.csv'
    INTO TABLE `data_to_load`
    FIELDS
        TERMINATED BY ','
        OPTIONALLY ENCLOSED BY '"'
        ESCAPED BY '"'
(
    `id`,
    `count`
);

Finally, UPDATE the existing data, by JOINing with your new table:

UPDATE
    `table_to_update`
    INNER JOIN `data_to_load`
        ON `data_to_load`.`id` = `table_to_update`.`id`
SET
    `table_to_update`.`count` = `table_to_update`.`count` + `data_to_load`.`count`
;

Answer:

Import your values from the csv using a technique like this one. Once you have that table, try this. If your original table is called “counts”, and your new table is called “adds”, this query will show you what the combination will be:

select a.id,a.count,b.add,a.count + b.add as sum from counts as a, adds as b where a.id = b.id;

If it’s doing what you want, then update:

update counts set counts.count = counts.count + adds.add where counts.id = adds.id;