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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:43:55+00:00 2026-06-11T12:43:55+00:00

Working on an existing webapp (PHP/MySQL) I came to this point: I have 2

  • 0

Working on an existing webapp (PHP/MySQL) I came to this point:
I have 2 tables storing names and types id. The relation between them being N<—->N I have another table in between. (see picture)

Tables names and columns have been simplified, but it's pretty much it

I have a last table called “category” which is unpictured. What I have to do is:
duplicate contracts and customers having id_category=1 to contracts and customers with same values, having id_category=3. As well as duplicating the relations between them.

So far I’ve managed to duplicate all contracts and customers apart, using:

INSERT INTO contract (t_name, id_category) SELECT t_name,'3' WHERE id_category=2
INSERT INTO customer (t_name, id_category) SELECT t_name,'3' WHERE id_category=2

(The “id” field being Auto-Incremented, it goes fine).

But about copying the relations, I can’t figure out HOW to take the problem.

Additional info:

  • Using temporary tables is fine, but I can’t modify the existing structure of
    tables.
  • Contracts may have from 0 to N customers. Customers may have
    from 0 to N contracts.
  • I can use either strict MySQL, or with PHP.
  • 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-11T12:43:57+00:00Added an answer on June 11, 2026 at 12:43 pm

    You say you can’t change the table structure. If you could, I would likely recommend against the denormalization (duplication of rows) of these tables depending upon what you are doing.

    I’m also a bit confused because you say you have to duplicate the rows and relations with id_category = 1 to id_category = 3, but then your sample queries have id_category = 2.

    This answer should apply regardless, though. I would use PHP and PDO.

    $pdo = new PDO('mysql:host=?', $user, $passwd);
    $stmtCustomers = $pdo->prepare("INSERT INTO customer (t_name, id_category)
       VALUES (t_name, ?) WHERE id_category = ?");
    $stmtContracts = $pdo->prepare("INSERT INTO contract (t_name, id_category)
       VALUES (t_name, ?) WHERE id_category = ?");
    $stmtRelation = $pdo->prepare("INSERT INTO customer_has_contract VALUES (?, ?)");
    
    //Perform in a loop if needed
    $pdo->beginTransaction();
    $stmtCustomers->execute($target_cat_id, $origin_cat_id);
    $cus_id = $pdo->lastInsertId();
    $stmtContracts->execute($target_cat_id, $origin_cat_id);
    $con_id = $pdo->lastInsertId();
    $stmtRelation->execute($con_id, $cus_id);
    $pdo->commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a PHP-based webapp that has an existing MySQL database where all
I am working on existing project. There are many forms like this: <?php echo
Real problem with an existing webapp which has been working for the last couple
I'm working on some existing code. After a few hours, I've boiled this problem
I'm working with an existing Android app and I need to have a dynamically
I have an existing working iPhone project inside XCode 4.2. I localized an image
I am working on existing project.Getting records something like this: $this->courses = Doctrine_Core::getTable('Course')->findByUserId($this->userId); Now
I'm working on developing a webapp that will replace an existing PowerBuilder app. They
I am working on existing project. I have following two URLs http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/1/?hcid=147 http://example.dev/en/course/myUserName/myCourseName/150/myChapterName/1016/page/2/?hcid=147 2nd
I have an existing java webapp that uses Hibernate for it's persistence. I've been

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.