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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:31:15+00:00 2026-06-11T14:31:15+00:00

How do i join 3 or more tables in mysql as follows? there is

  • 0

How do i join 3 or more tables in mysql as follows?

  • there is a column for each column of each table (except ID)
  • ID field values all go into the same ID field in the new table
  • an additional column is added called table the values of which is the source Table name
  • an autoincremented newID field is added
  • only one table contributes to each row, unrelated fields have null values
  • total number of rows is equal to the sum records from all tables

example with just two tables :

TableA:                  TableB          
ID   |   fieldA          ID   |   fieldB 
-----------------       -----------------
  1  |   valueA1           1  |   valueB1
  2  |   valueA2           2  |   valueB2


ResultTable:
newID  | ID  |  table  | fieldA  |  fieldB
---------------------------------------------
   1   |  1  |  TableA | valueA1 |  
   2   |  2  |  TableA | valueA2 |  
   3   |  1  |  TableB |         |  valueB1
   4   |  2  |  TableB |         |  valueB2

I know this probably sounds a bit weird!. I am going to try and use this to batch insert nodes for records from various tables into neojs graph database with this batch-insert script. which could be hilarious considering I hardly know what I am doing in either database 😉 .

  • 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-11T14:31:16+00:00Added an answer on June 11, 2026 at 2:31 pm


    Try this one,

    SELECT  @rownum := @rownum + 1 AS NewID,
            a.*
    FROM
        (
            SELECT  ID, fieldA, '' AS fieldB
            FROM    tableA
            UNION ALL
            SELECT  ID, '' AS fieldA, fieldB
            FROM    tableB
        ) a, (SELECT @rownum:=0) r
    

    SQLFiddle Demo

    • Create New Table

    here’s the proposed schema

    CREATE TABLE Newtable
    (
        NewID INT AUTO_INCREMENT,
        ID INT NOT NULL,
        FieldA VARCHAR(30),
        FieldB Varchar(30),
        CONSTRAINT tb_pk PRIMARY KEY (NewID)
    )
    
    • then Insert your values,

    here’s the query using INSERT INTO...SELECT statement

    INSERT INTO NewTable (ID, fieldA, fieldB)
    SELECT  ID, fieldA, NULL AS fieldB
    FROM    tableA
    UNION ALL
    SELECT  ID, NULL AS fieldA, fieldB
    FROM    tableB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MYSQL tables as follows user TABLE {id INT} profile TABLE {user_id INT,
Can I use the USING clause to join more than 2 tables? i.e. can
Is it a good idea to join three or more tables together as in
I have a table with more than 100 columns. I need to join it
I'm getting Error Code: 1054. Unknown column 'E1.extensao' in 'field list'. I changed table
I want to join two tables together and have ONLY the data in Table
This question follows on from MYSQL join results set wiped results during IN ()
I have two tables: bpoint -> table of all houses images -> table of
I have vps Mem:524 (215used) Swap:524(75used) , mysql 5.1.61 with three tables. Table 'new'
I'm trying to delete records from a table if there are more than 15

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.