Search code examples
mysqlsql-updatewhere-clauseclause

Splitting large column in table to another table of smaller columns MYSQL


I've been pulling my hair out trying to split a large column in a table (1.7 million rows) down into 24 much smaller columns in a different table.

The table from is: "postcodes" which contains the column to be split "postcode" and an auto increment "id" column

& the table to is: "postcodes_spit" which contains 24 columns called "postcoden" (insert 1-24 in n) ;-)

I can INSERT in the first of 24 columns fine:

INSERT INTO postcodes_split (postcodes1)
SELECT postcode 
FROM postcodes 
WHERE (id <= 72974);

but subsequent INSERTS of course don't start from row 0 in the other columns.

I tried to do UPDATE instead but I've got the syntax wrong and I can't seem to make it right:

UPDATE postcodes_split 
SET postcodes2 = postcode FROM postcodes 
WHERE id FROM postcodes BETWEEN 72975 AND 145948

Hope someone can help me!


Solution

  • Here's an untested idea, which I would only suggest for a one-time operation like this:

     UPDATE postcodes_split pcs, postcodes pc
     SET pcs.postcodes2 = pc.postcode
     WHERE pcs.id = pc.id - 72974