SQL INSERT INTO来自多个表

时间:2013-12-05 15:58:55

标签: sql sql-server

这是我的表1:

NAME       AGE        SEX        CITY             ID

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

这是我的表2:

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

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

我希望有人可以帮助我

修改

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

NATIONALITY       ID

Canadian          1
American          2
French            3

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

7 个答案:

答案 0 :(得分:54)

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

答案 1 :(得分:10)

我建议您不要创建新表,只需使用组合两个表的视图,这样如果表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;

或者只是颠倒表格的顺序并使用LEFT JOIN(我发现这比正确的连接更合乎逻辑,但这是个人偏好):

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;

答案 2 :(得分:4)

尝试做:

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,这将插入表3中表1的每条记录,对于与表2中的连接条件匹配的记录,它也将插入它们的数字。

答案 3 :(得分:4)

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

 ALTER TRIGGER [dbo].[DEL_HIST]
 ON  [dbo].[PAYMENT]
 AFTER  DELETE
 AS 
 BEGIN


 DECLARE @User_op varchar(50) 
 DECLARE @RGNO varchar(50)

 DECLARE @PAYEUR varchar(50)
 DECLARE @DATESYS SMALLDATETIME 
 DECLARE @RG_DATE SMALLDATETIME 
 DECLARE @RG_Montant varchar(50)


 SELECT @PAYEUR = CT_NumPayeur FROM DELETED

 SELECT @RG_Montant = RG_Montant FROM DELETED
 SELECT @RG_DATE = RG_DATE FROM DELETED
SELECT @RGNO = RG_No FROM DELETED

DELETE FROM RECORDPAY  WHERE  
RG_NO=@RGNO and PAYEUR= @PAYEUR  and CAISSIER=@user_op and Montant=@RG_Montant 
END

答案 4 :(得分:3)

如果我理解正确,您应该能够在一个查询中执行此操作,将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

答案 5 :(得分:1)

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

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

答案 6 :(得分:0)

以下是一个示例,如果多个表没有公共标识,您可以自己创建,我使用1 as commonId创建公共标识,以便我可以内部加入它们:

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