【问题标题】:SQL INSERT INTO from multiple tables来自多个表的 SQL INSERT INTO
【发布时间】:2013-12-05 15:58:55
【问题描述】:

这是我的第一张桌子:

NAME       AGE        SEX        CITY             ID

Clara      22         f          New York         1
Bob        33         m          Washington       2
Sam        25         m          Boston           3

这是我的第二张桌子:

NUMBER       ID
555-1111     1
555-2222     2
555-3333     3

现在我想要一张表 3,它显示了所有信息:

NAME       AGE        SEX        CITY             ID        NUMBER

Clara      22         f          New York         1         555-1111
Bob        33         m          Washington       2         555-2222
Sam        25         m          Boston           3         555-3333

我首先尝试仅将表 1 中的值插入到表 3 中,然后我将表 2 中的值插入到表 3 中,其中 Id = Id 是内部连接。

INSERT INTO table3 { name, age, sex, city, id}
SELECT name, age, sex, city, id
FROM table 1



INSERT INTO table3 { name, age, sex, city, id, number}
SELECT name, age, sex, city, id, number
FROM table 2 p
INNER JOIN table 3 c ON c.Id = p.Id

但我得到的只是我的价值观的重复。我有 9 个条目,而不是 3 个条目,有些条目的数字为空,有些只有数字,其余的为空,有些是正确的。

希望有人能帮助我

编辑

如果我现在有第三张这样的表:

NATIONALITY       ID

Canadian          1
American          2
French            3

如何将所有 3 个表合并为一个表?

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    您只需要一个 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
    

    【讨论】:

    • 效果很好!但是,如果我从 2 个以上的表中获取值,我该怎么办?查看我的编辑
    • 谢谢,那个对我有用。仅供参考,我需要 LEFT JOIN 因为在某些列中我有空值。
    • 我花了几秒钟才明白这个答案。插入(第一)行中的“table3”指的是与最后一个内部连接不同的“table3”。我建议更新表名。也许在插入中我们可以有:“destination_table”并在选择和连接“source_table1,2,3”上。谢谢。
    【解决方案2】:

    我建议不要创建新表,而只需使用组合两个表的视图,这样如果表 1 或表 2 中的任何数据发生更改,则无需更新第三个表:

    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;
    

    如果您可以在一个表中有记录,而另一个表中没有,那么您需要使用完全联接:

    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;
    

    如果您知道所有记录都在表 1 中,而只有一些记录在表 2 中,那么您应该使用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;
    

    如果您知道所有记录都在表 2 中,而只有一些记录在表 2 中,那么您可以使用 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;
    

    或者只是颠倒表格的顺序并使用左连接(我觉得这比右连接更合乎逻辑,但这是个人喜好):

    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;
    

    【讨论】:

    • 拒绝选民发表评论?我很想听到一个很好的论点,说明为什么一个观点不合适,或者提出的观点有什么问题。
    • 太好了,谢谢,但我想不需要视图,因为我不再需要旧表,我只是想把所有东西都放在一个表中。
    【解决方案3】:

    尝试做:

    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
    

    通过使用LEFT JOIN,这会将表1中的每条记录插入到table3中,并且对于table2中符合连接条件的记录,它也会插入它们的编号。

    【讨论】:

      【解决方案4】:

      这是对 D Stanley 答案的 3 个或更多表的简短扩展:

      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
      

      【讨论】:

        【解决方案5】:

        如果我对您的理解正确,您应该能够在一个查询中执行此操作,将 table1 和 table2 连接在一起:

        INSERT INTO table3 { name, age, sex, city, id, number}
        SELECT p.name, p.age, p.sex, p.city, p.id, c.number
        FROM table1 p
        INNER JOIN table2 c ON c.Id = p.Id
        

        【讨论】:

          【解决方案6】:

          要以预定义的方式显示 2 个表中的值,请使用 VIEW

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

          【讨论】:

            【解决方案7】:

            这里是一个例子,如果多个表没有公共ID,你可以自己创建,我使用1 as commonId创建公共ID,以便我可以内部加入它们:

            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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-12-10
              • 2011-05-13
              • 2015-07-29
              • 2016-04-23
              • 2021-11-19
              • 2022-01-05
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多