Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:38:43+00:00 2026-05-28T07:38:43+00:00

In MySQL, I am converting a table from a single row per item type

  • 0

In MySQL, I am converting a table from a single row per item type (a quantity of items) to a single row per item, so that additional detail can be stored about individual items.

Here is an example source table:

id    parent_id    qty    item_type
--    ---------    ---    ---------
1     10291        2      widget
2     10292        4      thinger

I want to create a new table with a new column containing info that cannot be applied to more than one item. Thus, the above table would end up as follows:

id    parent_id    item_type    info
--    ---------    ---------    ----
1     10291        widget       [NULL]
2     10291        widget       [NULL]
3     10292        thinger      [NULL]
4     10292        thinger      [NULL]
5     10292        thinger      [NULL]
6     10292        thinger      [NULL]

Is there a way I can iterate or loop each row of the source table, inserting a number of records equal to the source qty column? I would prefer to do this in sql instead of code to keep all of the conversion steps together (there are many others).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:38:44+00:00Added an answer on May 28, 2026 at 7:38 am

    Based on other answers which provided some insight, I was able to find additional information (by Kevin Bedell) to create a stored procedure and use a cursor in a loop. I have simplified my solution so that it matches the example in my question:

    DROP PROCEDURE IF EXISTS proc_item_import;
    DELIMITER $$
    CREATE PROCEDURE proc_item_import()
    BEGIN
        # Declare variables to read records from the cursor
        DECLARE parent_id_val INT(10) UNSIGNED;
        DECLARE item_type_val INT(10) UNSIGNED;
        DECLARE quantity_val INT(3);
    
        # Declare variables for cursor and loop control
        DECLARE no_more_rows BOOLEAN;
        DECLARE item_qty INT DEFAULT 0;
    
        # Declare the cursor
        DECLARE item_cur CURSOR FOR
            SELECT
                i.parent_id, i.qty, i.item_type
            FROM items i;
    
        # Declare handlers for exceptions
        DECLARE CONTINUE HANDLER FOR NOT FOUND
        SET no_more_rows = TRUE;
    
        # Open the cursor and loop through results
        OPEN item_cur;
    
        input_loop: LOOP
    
            FETCH item_cur
            INTO parent_id_val, item_type_val, quantity_val;
    
            # Break out of the loop if there were no records or all have been processed
            IF no_more_rows THEN
                CLOSE item_cur;
                LEAVE input_loop;
            END IF;
    
            SET item_qty = 0;
    
            qty_loop: LOOP
    
                INSERT INTO items_new
                    (parent_id, item_type)
                SELECT
                    parent_id_val, item_type_val;
    
                SET item_qty = item_qty + 1;
    
                IF item_qty >= quantity_val THEN
                    LEAVE qty_loop;
                END IF;
    
            END LOOP qty_loop;
    
        END LOOP input_loop;
    END$$
    DELIMITER ;
    

    Before asking this question, I had not used a stored procedures, cursors, or loops. That said, I have read and encountered them frequently on SE and elsewhere, and this was a good opportunity to learn

    It may be worth noting that the example on Kevin’s page (linked above) does not use END%% (just END) which caused some headache in trying to get the script to work. When creating a procedure, it is necessary to change the delimiter temporarily so that semicolons terminate statements inside the procedure, but not the creation process of the procedure itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, my mysql table is converting single and double quotes into strange
i am converting from access to mysql i have a table in access where
What is the php script for converting a mysql table to text file? I
Converting a couple stored procedures from MySQL to Microsoft SQL server. Everything is going
I am converting all my SQL Server queries to MySQL and my queries that
I'm converting a PHP script as DB has been switched from MySQL to PostgreSQL.
I imported a table from a CSV file into MySQL using this query: LOAD
I have a question about converting charset from inside mysql query. I have a
I'm converting a webapp from mysql to SQL Server. Now I want to convert
I need some help in converting my script from using mysql to mysqli I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.