Jump to content

Moving data from one table to two new tables


TeNDoLLA

Recommended Posts

Hello,

I need help moving data between the old db structure and the new db structure.

 

 

Old db structure (1 table):

table synonyms
----------------
synonym_id (auto_increment)
item_id
item_name
note_fin
note_eng
note_swe

 

New db structure (2 tables, 3 tables if we count the language table also, which defines the lang_id):

table synonyms
----------------
synonym_id (auto_increment)
item_id
item_name


table synonym_notes
----------------------
synonym_note_id (auto_increment)
synonym_id
note
lang_id

 

The problem is how to move with SQL commands the old data from one old table to the 2 new tables. I should get item_id and item_name from the old table to the new synonyms table. I should also move the corresponding notes to the new synonym_notes table for the 3 languages in a way that the synonym_id in the synonym_notes table refers to the synonym_id in the new synonyms table for the  corresponding items. lang_id's in the new structure are like: fin=1, eng=3, swe=4. Anyone can help me with this transition?

Link to comment
Share on other sites

I guess this could have been done easiest with some sort of bubblegum loop in PHP, but I wanted to try and do it in SQL. I guess I was thinking too complicated and tried to make it in loop inside a stored procedure. Problem was solved with just a simple joins, probably not the best way but did what I wanted it to do.

 

/* Query to get all synonyms from the old table to the new synonyms table */
INSERT INTO new_synonyms (item_id, item_name)
SELECT item_id, item_name FROM old_synonyms;


/* Below is queries to get all the note translations from the old synonym table
   to the new synonym note translations table. */
   
/* for finnish notes */
INSERT INTO new_synonym_notes (synonym_id, lang_id, note) 
SELECT t2.synonym_id, 1, s.synonym_note_fin FROM new_synonyms t2
JOIN old_synonyms s ON s.item_name = t2.item_name;

/* for english notes */
INSERT INTO new_synonym_notes (synonym_id, lang_id, note) 
SELECT t2.synonym_id, 3, s.synonym_note_eng FROM new_synonyms t2
JOIN old_synonyms s ON s.item_name = t2.item_name;

/* for swedish notes */
INSERT INTO new_synonym_notes (synonym_id, lang_id, note) 
SELECT t2.synonym_id, 4, s.synonym_note_swe FROM new_synonyms t2
JOIN old_synonyms s ON s.item_name = t2.item_name;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.