1

I have this table:

id      name     code
---------------------------
1        n1  
2        n2
3        n3

and I have these values that need to be written to the code column of every row:

('8n8kKaVu','SRE2vbpQ','Vdfb7V7s')

Also, it does not matter which row a value from the above table gets set to. So lets say i want the final table to look like this:

id     name     code
-----------------------
1      n1       8n8kKaVu
2      n2       SRE2vbpQ
3      n3       Vdfb7V7s
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
dave
  • 14,991
  • 26
  • 76
  • 110
  • possible duplicate of [mysql how to update a column of every row with a given set of values](http://stackoverflow.com/questions/31237117/mysql-how-to-update-a-column-of-every-row-with-a-given-set-of-values) – Isaac Bennetch Jul 07 '15 at 21:48

1 Answers1

1

If you load all the codes into a table, you could create an auto-incrementing key:

alter table codes add id integer auto_increment key;

Then update the code column in the original table with the values in the codes table:

UPDATE original_table
SET code = (SELECT
                codes.code
            FROM codes
            WHERE original_table.id = codes.id);
Alex Woolford
  • 4,433
  • 11
  • 47
  • 80