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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:35:24+00:00 2026-05-20T06:35:24+00:00

How to insert into 2 tables result from a select statement. I have a

  • 0

How to insert into 2 tables result from a select statement.

I have a table with several data:

val1 val2 val3 .... valn
-------------------------
 12   21   54        78
 ..   ..    ..       ..

I have something like:
Select t1.val1, t1.val2, t2.val3, t2.val4 into table1 t1 , table2 t2 from tablename.

So I want val1, val2, being inserted into a new table with 2 fields like:

tabble1:
id fieldvalue
1   val1
2   val2

the same goes to val3 and val4. How can this be acomplished

tabble2:
id fieldvalue
1   val3
2   val4

Is this possible?

  • 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-20T06:35:25+00:00Added an answer on May 20, 2026 at 6:35 am

    Granted, it is difficult to understand what you are trying to accomplish. If you are in fact trying to insert rows into two different tables, as marc_s stated, you must use two insert statements.

    However, judging from your sample, it may not be the case that you are trying to insert into two tables but rather use two tables to insert into a third table where you transpose the data. If that is the case, then you can do it in a single statement:

    Insert MysteryTable( Id, fieldvalue )
    Select 1, val1
    From Table1
    Union All
    Select 2, val2
    From Table1
    Union All
    Select 3, val3
    From Table2 --assuming these come from Table2. Isn't clear in the OP
    Union All
    Select 4, val4
    From Table2 --assuming these come from Table2. Isn't clear in the OP
    

    Of course, if Table1 or Table2 has many rows, then you will obviously get many rows with the same Id value in your MysteryTable.

    Update given change to OP

    Given your clarification, what you seek can be done but requires two queries similar to the one above.

    Insert Table1( Id, fieldvalue )
    Select 1, val1
    From SourceTable
    Union All
    Select 2, val2
    From SourceTable
    
    Insert Table2( Id, fieldvalue )
    Select 1, val3
    From SourceTable
    Union All
    Select 2, val4
    From SourceTable
    

    Another variation which generates your id values would be:

    With NumberedItems As
        (
        Select val1 As val
        From SourceTable
        Union All
        Select val2
        From SourceTable
        )
    Insert Table1(id, fieldname)
    Select Row_Number() Over( Order By val ) As Num
        , val
    From SourceTable
    
    With NumberedItems As
        (
        Select val3 As val
        From SourceTable
        Union All
        Select val4
        From SourceTable
        )
    Insert Table2(id, fieldname)
    Select Row_Number() Over( Order By val ) As Num
        , val
    From SourceTable
    

    Btw, in the above example, I used Union All, however if you are trying to normalize the data, you might want to have distinct values. In that case, you would use Union instead of Union All.

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

Sidebar

Related Questions

I have the following sample of data to insert into tables(from parent to child,
I am trying to select data from two tables and insert it into another
I have a long running insert transaction that inserts data into several related tables.
I have the following query (all tables are innoDB) INSERT INTO busy_machines(machine) SELECT machine
The need to insert values from 3 tables into another table called myTable .
I have the following code: $sql = INSERT INTO table VALUES ('', ...); $result
I'm inserting records from one table into another table using a select statement. The
I have two tables (in postgres) - ads and logs. After every insert into
I have data that looks like: x1 y1 z1 x2 y2 z2 val1 val2
I have to insert many product value records into an Oracle table. I get

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.