SQL是否只复制表1中不存在的数据?
我有2个sql表,我想合并成1个。我只想复制表1中“Item”列在表2中不存在的行。有什么想法吗SQL是否只复制表1中不存在的数据?,sql,select,Sql,Select,我有2个sql表,我想合并成1个。我只想复制表1中“Item”列在表2中不存在的行。有什么想法吗 Select Item into new table2 from table1 where???? 假设SQL Server在您的问题中给出了选择进入: 使用示例查询仅使用表1中的记录填充新表,其中项值不在表2中: SELECT a.Item INTO new_table2 FROM table1 a LEFT JOIN Table2 b ON a.item = b.item WHERE b
Select Item
into new table2
from table1
where????
假设SQL Server在您的问题中给出了
选择进入
:
使用示例查询仅使用表1
中的记录填充新表,其中项
值不在表2中
:
SELECT a.Item
INTO new_table2
FROM table1 a
LEFT JOIN Table2 b
ON a.item = b.item
WHERE b.item IS NULL
如果您不需要新表,只想将Table2
中不存在的Table1
记录添加到Table2
中:
INSERT INTO Table2 (Item)
SELECT a.Item
FROM table1 a
LEFT JOIN Table2 b
ON a.item = b.item
WHERE b.item IS NULL
请把它分成几步。如何编写select语句来提供所需的数据。现在使用select语句,将其设置为
createtable as
或插入selectfrom
INSERT INTO table2(col1, col2, col3) -- add the respective columns here
SELECT col1, col2, col3
FROM table1 A
WHERE NOT EXISTS(SELECT 1 FROM table2
WHERE Item = A.Item)