SQL INSERT INTO from multiple tables

前端 未结 7 624
清歌不尽
清歌不尽 2020-12-02 12:17

this is my table 1:

NAME       AGE        SEX        CITY             ID

Clara      22         f          New York         1
Bob        33         m                 


        
相关标签:
7条回答
  • 2020-12-02 12:56

    I would suggest instead of creating a new table, you just use a view that combines the two tables, this way if any of the data in table 1 or table 2 changes, you don't need to update the third table:

    CREATE VIEW dbo.YourView
    AS
        SELECT  t1.Name, t1.Age, t1.Sex, t1.City, t1.ID, t2.Number
        FROM    Table1 t1
                INNER JOIN Table2 t2
                    ON t1.ID = t2.ID;
    

    If you could have records in one table, and not in the other, then you would need to use a full join:

    CREATE VIEW dbo.YourView
    AS
        SELECT  t1.Name, t1.Age, t1.Sex, t1.City, ID = ISNULL(t1.ID, t2.ID), t2.Number
        FROM    Table1 t1
                FULL JOIN Table2 t2
                    ON t1.ID = t2.ID;
    

    If you know all records will be in table 1 and only some in table 2, then you should use a LEFT JOIN:

    CREATE VIEW dbo.YourView
    AS
        SELECT  t1.Name, t1.Age, t1.Sex, t1.City, t1.ID, t2.Number
        FROM    Table1 t1
                LEFT JOIN Table2 t2
                    ON t1.ID = t2.ID;
    

    If you know all records will be in table 2 and only some in table 2 then you could use a RIGHT JOIN

    CREATE VIEW dbo.YourView
    AS
        SELECT  t1.Name, t1.Age, t1.Sex, t1.City, t2.ID, t2.Number
        FROM    Table1 t1
                RIGHT JOIN Table2 t2
                    ON t1.ID = t2.ID;
    

    Or just reverse the order of the tables and use a LEFT JOIN (I find this more logical than a right join but it is personal preference):

    CREATE VIEW dbo.YourView
    AS
        SELECT  t1.Name, t1.Age, t1.Sex, t1.City, t2.ID, t2.Number
        FROM    Table2 t2
                LEFT JOIN Table1 t1
                    ON t1.ID = t2.ID;
    
    0 讨论(0)
  • 2020-12-02 12:58

    Try doing:

    INSERT INTO table3(NAME,AGE,SEX,CITY,ID,NUMBER)
    SELECT t1.name,t1.age, t1.sex,t1.city,t1.id,t2.number
    FROM table1 t1
    LEFT JOIN table2 t2 ON t1.id = t2.id
    

    By using LEFT JOIN, this will insert every record from table 1 in table3, and for the ones that match the join condition in table2, it will also insert their number.

    0 讨论(0)
  • 2020-12-02 13:00

    You only need one INSERT:

    INSERT INTO table4 ( name, age, sex, city, id, number, nationality)
    SELECT name, age, sex, city, p.id, number, n.nationality
    FROM table1 p
    INNER JOIN table2 c ON c.Id = p.Id
    INNER JOIN table3 n ON p.Id = n.Id
    
    0 讨论(0)
  • 2020-12-02 13:05

    Here is an example if multiple tables don't have common Id, you can create yourself, I use 1 as commonId to create common id so that I can inner join them:

    Insert Into #TempResult
    select CountA, CountB, CountC  from
    
    (
    select Count(A_Id) as CountA, 1 as commonId from tableA
      where ....
      and  ...
      and   ...
    ) as tempA
    
    inner join
    (
    select Count(B_Id) as CountB, 1 as commonId from tableB
      where ...
      and ...
      and  ...
     ) as tempB
    
    on tempA.commonId = tempB.commonId
    
    inner join
    (
    select Count(C_ID) as CountC, 1 as commonId from tableC
    where ...
    and   ...
    ) as tempC
    
    on tmepB.commonId = tempC.commonId
    
    --view insert result
    select * from #TempResult
    
    0 讨论(0)
  • 2020-12-02 13:06

    Here is an short extension for 3 or more tables to the answer of D Stanley:

    INSERT INTO other_table (name, age, sex, city, id, number, nationality)
    SELECT name, age, sex, city, p.id, number, n.nationality
    FROM table_1 p
    INNER JOIN table_2 a ON a.id = p.id
    INNER JOIN table_3 b ON b.id = p.id
    ...
    INNER JOIN table_n x ON x.id = p.id
    
    0 讨论(0)
  • 2020-12-02 13:06

    To show the values from 2 tables in a pre-defined way, use a VIEW

    http://www.w3schools.com/sql/sql_view.asp

    0 讨论(0)
提交回复
热议问题