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 6690697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:40:01+00:00 2026-05-26T05:40:01+00:00

It is not as easy as the title itself. I have a table users

  • 0

It is not as easy as the title itself. I have a table users which has a field assignedlessons. Data stored in this field is like 69|308|50|91. As you may have already know, it keep several lessons of a user has at the same time. What I am going to do is to export data from this field along with user_id and import to another newly created table user_assigned_elearning_lessons. The structure of this table is: id, user_id, elearning_lesson_id, created_at. After the importing, the structure in the new table should be like this:

id        user_id       elearning_lesson_id        created_at
1         1             69                         2011-01-12
2         1             308                        2011-04-11
3         2             50                         2011-05-18
4         3             91                         2011-05-21
5         3             50                         2011-07-18
6         3             308                        2011-07-18

How do I do that?

  • 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-26T05:40:02+00:00Added an answer on May 26, 2026 at 5:40 am

    Despite what others have suggested, you can’t do what you want with INSERT ... SELECT syntax, because you’ve to split values and insert more than one row for each row of the source table.

    You can instead write a short PHP script to do the job, I’ve assumed the following table structure and test data:

    CREATE TABLE IF NOT EXISTS `users` (
    `user_id` int(11) NOT NULL,
    `assignedlessons` varchar(100) NOT NULL,
    PRIMARY KEY (`user_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    
    INSERT INTO `users` (`user_id`, `assignedlessons`) VALUES
    (1, '69|308|50|91'),
    (2, '56|34|7');
    
    CREATE TABLE IF NOT EXISTS `user_assigned_elearning_lessons` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) NOT NULL,
    `elearning_lesson_id` int(11) NOT NULL,
    `created_at` date NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
    

    With a PHP script you can loop over all the rows of the first table, explode the composite field into its parts and do an INSERT statement for each of those part. I’ve used mysqli object oriented style with prepared statements to build the insert queries.

    <?php
    main();
    
    function main() {
        $source = new mysqli("localhost", "username", "password", "database");
        $destin = new mysqli("localhost", "username", "password", "database");
        $stmt = prepareInsertStatement($destin);
        $result = $source->query("SELECT user_id, assignedlessons FROM users");
        while ($user = $result->fetch_object()) {
            insertUser($stmt, $user);
        }
        $result->close();
        $source->close();
        $destin->close();
    }
    
    function insertUser(&$stmt, &$user) {
        $lessons = explode('|', $user->assignedlessons);
        foreach ($lessons AS $lesson) {
            $stmt->bind_param("ii", $user->user_id, $lesson);
            $stmt->execute();
            echo "User " . $user->user_id . " lesson $lesson<br/>";
        }
    }
    
    function &prepareInsertStatement(&$destin) {
        $sql = "INSERT INTO user_assigned_elearning_lessons
            (user_id, elearning_lesson_id, created_at) VALUES (?, ?, NOW())";
        $stmt = $destin->stmt_init();
        $stmt->prepare($sql);
        return $stmt;
    }
    ?>
    

    I bind the parameters to the the prepared statement with bind_param:

    $stmt->bind_param("ii", $user->user_id, $lesson);
    

    Where the "ii" means that the type of those parameters is integer and integer.

    The output with this test data will be:

    User 1 lesson 69
    User 1 lesson 308
    User 1 lesson 50
    User 1 lesson 91
    User 2 lesson 56
    User 2 lesson 34
    User 2 lesson 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the title, not easy to phrase it correctly. I have a table
OK. The title of this qiestion is not easy to decode, but let me
I have this kind of HTML structure (stripped down for easy readability): <head> <title>My
The title makes it sound easy, but I guarantee it's not. I have a
Consider I have a Task class like this : class Task { String title
This is an Easy Q, but great help: Under title Retrieving data from threads
Hi guys I have an easy question, but not easy to me. I have
I have to modify SQL Server reports. It is not easy to modify directly
This is most likely not an easy one but here is the situation: I
Before answering, it is not as easy question as you might have thought about

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.