Sample Header Ad - 728x90

Merging Data From Multiple Tables With Nesting/Loops

-1 votes
2 answers
83 views
Long story short I need to replace a website component with a newer one. Problem is the way the old one stored information in the database is very convoluted. I have a new empty table (for the new component) with 3 columns: | field_id | item_id | value | Each value has a different item_id but field_id is a static number (2). I can populate the table from the old component's table (where x is the data I am unable to get from the old table):
INSERT INTO new_table (field_id, item_id, value)
    SELECT 2, x , content FROM old_table WHERE condition;
The problem is item_id (x above), the old component has this value stored in a different table but the only thing linking the two values is an asset_id from ANOTHER table. So for each row I somehow need to do the following: 1. Get a value called asset_id from old_table_form, 2. Then in a table old_table_content I need the value item-id from the same row as asset_id, 3. Save this value in new_table as item_id for each row. I thought maybe nesting SELECT commands could work but I am having trouble even visualizing how it should be, or maybe a loop with pseudo-code something like:
LOOP
    DECLARE var1
    DECLARE var2
    SELECT FROM old_table_form -> 'asset_id'
    var1 = asset_id
    SELECT FROM old_table_content -> 'item_id' from 'asset_id' row
    var2 = 'item_id'
    save var2 in new table as 'item_id'
    repeat for all rows in table
    END LOOP
Is something like this even possible in MySQL/phpmyadmin? Any ideas/advice would be greatly appreciated.
Asked by rudiments (7 rep)
Oct 18, 2024, 11:50 PM
Last activity: Oct 19, 2024, 06:33 PM