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

  • Home
  • SEARCH
  • 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 8189067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:09:09+00:00 2026-06-07T03:09:09+00:00

I have a MySQL table containing several fields; like this: id name ton dyr

  • 0

I have a MySQL table containing several fields; like this:

id  name  ton           dyr
1   moo   FRET_GTR_HYU  ra
2   maa   DER_GY_TR_DB  fe
3   tee   SAME_IQ       tete
...

The question is that the contents for every row of the column ton are independent, but they are joined with a symbol “_”.

I would like to modify the table, splitting the field ton to end up keeping all the same entries in the other fields, but just one entry per row in the ton field, like this…

id  name  ton      dyr
1   moo   FRET     ra
1   moo   GTR      ra
1   moo   HYU      ra
2   maa   DER      fe
2   maa   GY       fe
2   maa   TR       fe
2   maa   DB       fe
3   tee   SAME     tete
3   tee   IQ       tete

...

I tried to modify the code given in this page to perform the job, but as i’m not that expert in MySQL, i am not able to get it to work my way. In the link above, the code is given suitable to rewrite the two columns given, not any other fields.

Any help on how to modify the code, or do it another way?

  • 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-06-07T03:09:11+00:00Added an answer on June 7, 2026 at 3:09 am

    1) Copy table1 structure to table2

    2) Add the 2 stored procedures to your database.

    CREATE PROCEDURE peixe()
    BEGIN
      DECLARE done BOOLEAN DEFAULT 0;
      DECLARE i INT;
      DECLARE cursor_id CURSOR FOR SELECT id FROM table1 WHERE ton LIKE '%_%';
      DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
      OPEN cursor_id;
      FETCH cursor_id INTO i;
      REPEAT
        CALL split_ton(i);
        FETCH cursor_id INTO i;
      UNTIL done END REPEAT;
      CLOSE cursor_id;
    END
    
    CREATE PROCEDURE split_ton(i INT)
    BEGIN
      DECLARE str_name VARCHAR(50);
      DECLARE str_ton VARCHAR(50);
      DECLARE str_ton_el VARCHAR(50);
      DECLARE str_dyr VARCHAR(50);
      DECLARE ton_size INT;
    
      SELECT name, ton, dyr
      INTO str_name, str_ton, str_dyr
      FROM table1 WHERE id=i;
    
      SET ton_size = LENGTH(str_ton) - LENGTH(REPLACE(str_ton, '_', '')) + 1;
    
      WHILE ton_size > 0 DO
        SET str_ton_el = SUBSTRING_INDEX(str_ton, '_', -1);
        SET str_ton = SUBSTRING_INDEX(str_ton, '_', ton_size - 1);
        INSERT INTO table2 (id, name, ton, dyr) VALUES (i, str_name, str_ton_el, str_dyr);
        SET ton_size = ton_size - 1;
      END WHILE;
    END
    

    3) Finally, execute the following statement :

    CALL peixe
    

    And I think, you’re done 🙂

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

Sidebar

Related Questions

I have a mysql table containing a field name dtt_date and have values like
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have MySQL InnoDB table utf-8 encoded. This table has only id and name
I have a MySQL table that looks something like this: +-----+------------+ | id |
In a MySQL table, I have several columns - some containing strings, some dates
I have a MySQL db with a table containing two fields: check_date (of DATETIME
I have a mysql database with table 'product' containing columns 'product_id', 'name', 'price' table
I have got a mysql table containing data about companies: id, company name, city,
I have a MySQL table containing event data. On this table, I have a
I have a MySQL MyISAM table containing entries that describe airports. This table contains

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.