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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:04:11+00:00 2026-06-05T17:04:11+00:00

I have two tables connected with one to many relationship. Parent Table is a

  • 0

I have two tables connected with one to many relationship.
Parent Table is a simple user table with id and first_name column

Parent

id | first_name     
1  | Bob  
2  | Dick  
3  | Harry  
4  | Tom  
5  | Holly  

Child Table contains insurance selection for a user.

Child

id | insu_id      | user_id   | year  
1  | 188765       | 1         | 2008  
2  | 188765       | 1         | 2009  
3  | 188765       | 1         | 2010   
4  | 188765       | 1         | 2011  
5  | 188765       | 1         | 2012  

I want to copy insurance selection of user_id 1 to all rest of the users i.e. 2, 3, 4, 5 in child table so that the table should look like

id | insu_id      | user_id   | year  
1  | 188765       | 1         | 2008   
2  | 188765       | 1         | 2009      
3  | 188765       | 1         | 2010  
4  | 188765       | 1         | 2011  
5  | 188765       | 1         | 2012  
6  | 188765       | 2         | 2008  
7  | 188765       | 2         | 2009   
8  | 188765       | 2         | 2010  
9  | 188765       | 2         | 2011   
10 | 188765       | 2         | 2012  
11 | 188765       | 3         | 2008  
12 | 188765       | 3         | 2009   
13 | 188765       | 3         | 2010  
14 | 188765       | 3         | 2011  
15 | 188765       | 3         | 2012  
16 | 188765       | 4         | 2008  
17 | 188765       | 4         | 2009   
18 | 188765       | 4         | 2010  
19 | 188765       | 4         | 2011  
20 | 188765       | 4         | 2012  
21 | 188765       | 5         | 2008  
22 | 188765       | 5         | 2009   
23 | 188765       | 5         | 2010  
24 | 188765       | 5         | 2011  
25 | 188765       | 5         | 2012  

What Can I do

INSERT INTO child(insurance_id, user_id, year) SELECT insurance_id, '2', year FROM child WHERE user_id = 1

INSERT INTO child(insurance_id, user_id, year) SELECT insurance_id, '3', year FROM child WHERE user_id = 1

INSERT INTO child(insurance_id, user_id, year) SELECT insurance_id, '4', year FROM child WHERE user_id = 1

INSERT INTO child(insurance_id, user_id, year) SELECT insurance_id, '5', year FROM child WHERE user_id = 1

What I want

I don’t want to run 4 different INSERT INTO() SELECT queries because number of users can increase beyond that. I want one query selecting user_id dynamically rather than hard coding.

  • 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-05T17:04:12+00:00Added an answer on June 5, 2026 at 5:04 pm

    I have tried that and it seems to work. I get the insurance data from child table for the first user and join it with all other users in the parent table in order to insert the result. You may have to fix the column names if they are not the same as your database.

    INSERT INTO child(insurance_id, user_id, year)
    SELECT a.insurance_id, b.id, a.year
    FROM
    child a, parent b
    WHERE a.user_id = 1 AND b.id > 1
    ORDER BY b.id, a.year
    

    This will always insert insurance data for all users except the one with id = 1. No matter how many of them you have. Hope that helps 🙂

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

Sidebar

Related Questions

I have a database with two different tables, connected with one to many relationship.
There are two tables, which have a relationship(one-to-many). There are Meals table which contains
I have two tables connected by a foreign key with a one to many
I have two tables, one parent Point and one child PointValue, connected by a
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have two tables. One table is meant to serve as a transaction history
I have two tables (groups and parties) in a many to many relationship using
I have set up a HABTM relationship between two table creating a many to

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.