SQL语句将2个表连接在一起并插入新表(sqlite)

时间:2018-10-06 05:36:23

标签: sql sqlite

好的,我有两个叫汽车和保险的桌子

汽车桌子的外观如下:

  1. reference_number
  2. 品牌
  3. 颜色

保险表类似于:

  1. 类型

  2. 引用编号

被保险人的表是这样的(首先是空的):

  1. 品牌

  2. 颜色

目标是将所有带保险的汽车导入被保险人表。

到目前为止,我所能想到的是:

"SELECT cars.reference_number, insured.reference_number FROM INSPECTIONS  LEFT JOIN insured ON cars.reference_number = cars.reference_number ORDER BY cars.reference_number")

1 个答案:

答案 0 :(得分:1)

根据您的解释(“所有带保险的汽车”)-被保险人也有reference_number,对吧?:

insert into insured (reference_number, brand, colour)
SELECT reference_number, brand, colour 
FROM Cars 
where reference_number in (select reference_number from insurance) 
ORDER BY reference_number