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?
You’ll need to do this in a couple of steps:
CREATEaTEMPORARY TABLE.LOADthe data into the temporary tableUPDATEthe original table, using aJOINagainst the temporary one.CREATE [TEMPORARY] TABLEsyntax 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 theCREATE ... (SELECT...)syntax, eg:This will create a
TEMPORARY TABLEcalleddata_to_load, consisting of exactly the same format as thetable_to_updatecolumnsidandcount. TheWHERE 0will 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:Finally,
UPDATEthe existing data, byJOINing with your new table: