Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL是否只复制表1中不存在的数据?_Sql_Select - Fatal编程技术网

SQL是否只复制表1中不存在的数据?

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

我有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.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)