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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:02:05+00:00 2026-06-13T05:02:05+00:00

My problem is very simple to explain but I don’t success to find the

  • 0

My problem is very simple to explain but I don’t success to find the right answer to it !

I have 3 tables in mysql, called tA, tB, tC. I have one entry A in tA, 2 entries B in tB and 3 entries C in tC with a hierarchical architecture like this:

            A
            |
     ----------------
     |              |
     B1             B2
     |              |
 --------           |
 |      |           |
 C1     C2          C3

As you can imagine, I have B entries are linked to A entries with A id, and C entries are linked to B entries with B id.

I just want to copy these data to :

            A'
            |
     ----------------
     |              |
     B1'            B2'
     |              |
 --------           |
 |      |           |
 C1'    C2'         C3'

So at the beginning, I start by creating A’ and copying the B entries linked to A’. No worries.

But after, I did a request with a left join to copy C entries to C’. It nearly works… the only problem is that, by this way, my C’ entries are linked to B id and not to B’ id !!!!

I try some other stuffs but I don’t know how to do that. It seems so simple. Maybe I’m tired… Maybe I have to copy B entries one by one, and copy all C entries of the current B entry before to go to the next.

But is there not a smarter way to do that ? With insert_id, cascade, trigger, foreign key ???

I’m not a sql specialist and I hope somebody here will have a nice solution.

Thanks by advance.

Bastien

  • 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-13T05:02:06+00:00Added an answer on June 13, 2026 at 5:02 am

    Alain’s method is a possible way to handle this purely in SQL, but requires some table modifications, basically storing the original id that an object was copied from.

    So you said you are using php, and I wish I could give you more relevant examples, but I’ve been away from php too long. But you should be able to do the following.

    1. Get A from the database, store A id in a variable in php (suggest $oldAID)
    2. Copy A properties and store back to the db as A’
    3. Get Last Insert ID and store as variable (suggest $newAID)
    4. Get all B records from DB where parent = $oldAID
    5. Loop through the b records, store the current records id as $oldBID
    6. Copy B1 propertied and store back to DB as B1′ with a parent id $newAID
    7. Get last insertID ans store as $newBID
    8. Get all C records from DB where parent ID = $oldBID
    9. Make Copies and store back to DB with parent ID = $newBID
    10. Itterate through your loop started in step 5

    By using PHP to keep tabs on your last insert ID’s you can make the copies you need and keep them accurate. Downside to this, there is a lot of back and forth between php and MySQL so in a really large set, that would go several levels deep, a process like this can take a few seconds. But if your normal sets are as small and shallow as your example, it shouldn’t be too bad.

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

Sidebar

Related Questions

I have a very simple problem but cannot find a nice solution. I have
I have a very simple problem, but I can't seem to solve it. I
I have very strange problem with mySQL and simple query with simple index. I
This problem seems very simple to me, but I've been unable to fix it,
I have a big problem with very simple code. I need to get a
I have a very simple problem. I have a data frame with multiple rows
Im quite sure there is a very simple fix to my problem, but i
My problem is very simple $('#button').removeAttr('type'); triggers an error on firebug type property can't
My problem is very simple, yet I feel lost while looking at it ...
Hello everyone my problem is very simple. Using entity framework code first I want

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.