如何在多个键上使用反向JOIN进行INSERT?

时间:2009-06-18 15:13:49

标签: sql sql-server

如何使用多个键列进行反向连接?

在这个婴儿玩具SqlServer示例中,我有以下

CREATE TABLE [dbo].[CarList](
 [myID] [int] IDENTITY(1,1) NOT NULL,
 [CarColour] [varchar](32) NOT NULL,
 [CarName] [varchar](128) NOT NULL,
 [CarCompany] [varchar](32) NOT NULL,

 CONSTRAINT [PK_CarList] PRIMARY KEY CLUSTERED(
  [myID] ASC,
  [CarColour] ASC,
  [CarName] ASC,
  [CarCompany] ASC
 )
)

GO

INSERT INTO CarList (CarColour, CarName, CarCompany)
VALUES('blue', 'Abe', 'Ford')

在DB的其他地方,我有一个像

这样的表
CREATE TABLE [dbo].[NewCars](
 [CarColour] [varchar](32) NOT NULL,
 [CarName] [varchar](128) NOT NULL,
 [CarCompany] [varchar](32) NOT NULL,
)

GO

INSERT INTO NewCars (CarColour, CarName, CarCompany)
SELECT 'blue', 'Abe', 'Ford'
 UNION ALL
SELECT 'blue', 'Abe', 'GM'
 UNION ALL
SELECT 'blue', 'Betty', 'Ford'
 UNION ALL
SELECT 'green', 'Abe', 'Honda'

现在我想在CarList表中插入我尚未拥有的汽车

像...一样的东西。

INSERT INTO CarList ( CarColour, CarName, CarCompany)
 SELECT DISTINCT new.CarColour, new.CarName, new.CarCompany
 FROM    NewCars new, CarList old
 WHERE   new.CarColour  <> old.CarColour
     AND new.CarName    <> old.CarName
     AND new.CarCompany <> old.CarCompany

哪个不起作用,因为“蓝色”,“贝蒂”,“福特”排将被过滤掉......

如果这只是某种ID,那真的很容易

INSERT INTO myTable (myID, param1, param2, etc)
SELECT param1, param2, etc
FROM someOtherTable new
WHERE new.myID NOT IN (SELECT myID FROM myTable)

由于我不想进入的原因,我无法从与CarList匹配的NewCars中删除行。如果可能的话,我也需要一次通过。

[编辑] 谢谢你们!

4 个答案:

答案 0 :(得分:5)

感谢DDL和DML

这是一种方式

INSERT INTO CarList ( CarColour, CarName, CarCompany)
 SELECT DISTINCT *
 FROM    NewCars n
where not exists (select 1 from CarList c where c.CarColour =n.CarColour
and c.CarName = n.CarName
and c.CarCompany = n.CarCompany)

至少有4种不同的方法可以做到这一点

  • NOT IN(不会像你一样工作超过1列)
  • NOT EXISTS
  • LEFT和RIGHT JOIN
  • 外部申请(2005 +)
  • 除(2005 +)

阅读Select all rows from one table that don't exist in another table

答案 1 :(得分:5)

INSERT
INTO    CarList ( CarColour, CarName, CarCompany)
SELECT  CarColour, CarName, CarCompany
FROM    NewCars nc
WHERE   NOT EXISTS
        (
        SELECT  1
        FROM    CarList cl
        WHERE   cl.CarColor = nc.CarColor
                AND cl.CarName  = nc.CarName
                AND cl.CarCompany = nc.CarCompany
        )

答案 2 :(得分:1)

INSERT INTO CarList ( CarColour, CarName, CarCompany) 
SELECT DISTINCT new.CarColor, new.CarName, new.CarCompany 
FROM    NewCar new
where not exists (select 0 
         from  CarList old 
         WHERE   new.CarColour  = old.CarColour     
         AND new.CarName    = old.CarName     
         AND new.CarCompany = old.CarCompany)
--This statement matches all that does exists in carlist
--and insert everything that does not exists in Carlist

答案 3 :(得分:1)

我可能会使用:

INSERT INTO CarList(CarColour, CarName, CarCompany)
SELECT
     NC.CarColour,
     NC.CarName,
     NC.CarCompany
FROM
     NewCars NC
LEFT OUTER JOIN CarList CL ON
     CL.CarColour = NC.CarColour AND
     CL.CarName = NC.CarName AND
     CL.CarCompany = NC.CarCompany
WHERE
     CL.MyID IS NULL