我有一个SQL Server Express数据库,其中包含一个我不想操作的系统生成的表,所以我创建了另一个表。
TABLE_1
AreaName
Area_1
Area_2
Area_3
Area_4
TABLE_2
Area_1
Area_2
这可以在一个声明中做到吗?
SELECT
MIN (TableName) AS TableName,
Area
FROM
(SELECT
'Table A' AS TableName,
Table_2.Area
FROM Table_2
UNION ALL
SELECT
'Table B' AS TableName,
Area
FROM [Table_1]) tmp
GROUP BY
Area
HAVING
COUNT(*) = 1
ORDER BY
Area;
这是我现在用来排序的SQL ......
答案 0 :(得分:1)
直截了当的做法:
INSERT INTO Table_2 (AreaName)
SELECT AreaName FROM Table_1
WHERE AreaName NOT IN (SELECT AreaName FROM Table_2)
答案 1 :(得分:0)
Insert into table1 (columns)
select Columns from table2 a
where not exists(select * from table3 b where a.ID =b.ID)