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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:34:24+00:00 2026-05-28T00:34:24+00:00

I need to copy data within a tab that has hierarchical relationships and maintain

  • 0

I need to copy data within a tab that has hierarchical relationships and maintain the relation within the new data.

I have a table “site_content”, the table has thousands of rows and the data in the table is quite extent. Here are the reduced table structure and column data for an example.

  CREATE TABLE `site_content` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `site_id` int(11) DEFAULT NULL,
 `parent` int(11) unsigned DEFAULT NULL,
 `parent_orig_id` int(11) unsigned DEFAULT NULL,
 `content_text` text,
PRIMARY KEY (`id`));

INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(1, 76, 0, NULL, '');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(2, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(3, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(4, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(5, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(6, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(7, 76, 2, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(8, 76, 3, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(9, 76, 3, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(11, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(12, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(13, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(14, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(15, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(28, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(29, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(30, 76, 5, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(32, 76, 5, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(36, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(41, 76, 32, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(42, 76, 32, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(43, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(44, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(45, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(46, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(47, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(48, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(49, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(50, 76, 42, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(51, 76, 42, NULL, 'some content');

I need to copy the “site 76” rows within the table, so I use this query to copy the table and also to give me the original parent child relationship information in the ‘parent_orig_id’ column.

INSERT INTO site_content (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`)
SELECT NULL, "200", `parent`, `id`, `content_text` FROM site_content 
WHERE `site_id` = '76';

I now have the data for site “76” copied in the table as site “200”

How can I update the new site 200 rows to have the same relationship as the site 76 rows? For example row “1” is the parent of rows “2, 3, 4, 5, 6”.

Row “2” for site 200 is now row id “53” and thinks that its parent is still row “1” based off of the “parent” column from the copy. How can I update the column “parent” in the copied rows to now reflect the new relationship as it is site 200. The “parent_orig_id” column hold the row id for the original hierarchical relationship.

I can see that if I do the query what I need to update. I would like to do this in SQL if possible.

select a.id current_row, a.parent as current_parent, b.id AS new_parent
  from site_content a, site_content b
 where b.parent_orig_id = a.parent AND a.site_id = '200' AND b.site_id = '200'
  • 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-28T00:34:24+00:00Added an answer on May 28, 2026 at 12:34 am

    You need to maintain not only the parent_orig_id, but also the orig_id. Since you’ve gone half the distance using the strategy of having columns in your table for this, I’ll continue in that direction. Like this:

    CREATE TABLE `site_content` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `site_id` int(11) DEFAULT NULL,
    `parent` int(11) unsigned DEFAULT NULL,
    `orig_id` int(11) unsigned DEFAULT NULL,
    `parent_orig_id` int(11) unsigned DEFAULT NULL,
    `content_text` text,
    PRIMARY KEY (`id`));
    
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(1, 76, 0, NULL, '');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(2, 76, 1, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(3, 76, 1, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(4, 76, 1, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(5, 76, 1, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(6, 76, 1, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(7, 76, 2, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(8, 76, 3, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(9, 76, 3, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(11, 76, 9, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(12, 76, 9, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(13, 76, 9, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(14, 76, 4, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(15, 76, 4, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(28, 76, 4, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(29, 76, 4, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(30, 76, 5, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(32, 76, 5, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(36, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(41, 76, 32, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(42, 76, 32, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(43, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(44, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(45, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(46, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(47, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(48, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(49, 76, 41, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(50, 76, 42, NULL, 'some content');
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`) VALUES(51, 76, 42, NULL, 'some content');
    
    
    INSERT INTO `site_content` (`id`, `site_id`, `parent`, `orig_id`,  `parent_orig_id`, `content_text`)
    SELECT NULL, "200", NULL, `orig_id`, `parent_orig_id`, `content_text` FROM site_content 
    WHERE `site_id` = '76';
    
    UPDATE site_content AS sc_target 
    LEFT JOIN site_content AS sc_source 
    ON sc_source.parent_orig_id = sc_target.parent 
    SET sc_target.parent = sc_source.id where sc_target.site_id = "200"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in an SQL Server 2005 database which I need to copy
I've one situation where I need to copy part of the data from one
I need to copy about 40 databases from one server to another. The new
I need to copy the newest file in a directory to a new location.
I am dealing with large amounts of scientific data that are stored in tab
I have some reference data in a text file (~5MB) that I want to
I need to copy data from one database to another using a VB.NET program.
(using SS2008) Within a sql server database, I'd like to copy all the data
I need to copy data from one database into my own database, because i
Im trying to copy data only between two SQL server 2008 databases. I need

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.