sql查询创建一个新表

时间:2011-09-23 02:20:58

标签: sql tsql

我有一张表如下: 表1

temp_id    node_name  variable_1 variable_2 variable_3
1          ab         a          b           y
2          sdd        a          a           a
3          u          a          s           s

和另一个表如下: 表2

temp_id    node_name  variable_1 variable_2 variable_3
1          ab         as        sb           y
2          sdd        a          a           a
3          u          a          s           s

我想从仅限表1 中获取所有记录,其中表1的组合variable_1,variable_2和variable_3与表2不匹配。 例如,在表1中,第一个记录有a,b,y(variable_1,variable_2和variable_3),而这在table2中不存在。

我如何在TSQL中执行此操作?

5 个答案:

答案 0 :(得分:2)

目前尚不清楚是否需要外部联接。我假设列都是NOT NULL限定的;如果任何variable_X列都可以保存空值,则条件会变得复杂得多。

SELECT T1.*
  FROM Table1 AS T1
  JOIN Table2 AS T2 ON T1.Temp_ID = T2.Temp_ID AND T1.Node_Name = T2.Node_Name
 WHERE T1.Variable_1 != T2.Variable_1
    OR T1.Variable_2 != T2.Variable_2
    OR T1.Variable_3 != T2.Variable_3;

如果允许NULLS,那么你必须写:

SELECT T1.*
  FROM Table1 AS T1
  JOIN Table2 AS T2 ON T1.Temp_ID = T2.Temp_ID AND T1.Node_Name = T2.Node_Name
 WHERE (T1.Variable_1 != T2.Variable_1
        OR (T1.Variable_1 IS NULL AND T2.Variable_1 IS NOT NULL)
        OR (T2.Variable_1 IS NULL AND T1.Variable_1 IS NOT NULL)
       )
    OR (T1.Variable_2 != T2.Variable_2
        OR (T1.Variable_2 IS NULL AND T2.Variable_2 IS NOT NULL)
        OR (T2.Variable_2 IS NULL AND T1.Variable_2 IS NOT NULL)
       )
    OR (T1.Variable_3 != T2.Variable_3
        OR (T1.Variable_3 IS NULL AND T2.Variable_3 IS NOT NULL)
        OR (T2.Variable_3 IS NULL AND T1.Variable_3 IS NOT NULL)
       );

请注意,这与:

不同
SELECT T1.*
  FROM Table1 AS T1
  JOIN Table2 AS T2 ON T1.Temp_ID = T2.Temp_ID AND T1.Node_Name = T2.Node_Name
 WHERE NOT (T1.Variable_1 = T2.Variable_1
       AND  T1.Variable_2 = T2.Variable_2
       AND  T1.Variable_3 = T2.Variable_3);

答案 1 :(得分:0)

select * from table1 t1, table2 t2 where
t1.temp_id = t2.temp_id and
( t1.var1 != t2.var1 OR t1.var2 != t2.var2 OR t1.var3 != t2.var3)

答案 2 :(得分:0)

select * from table1 t1
left outer join table2 t2 on t1.temp_id = t2.temp_id 
where  t1.var1 <> t2.var1 OR t1.var2 <> t2.var2 OR t1.var3 <> t2.var3

答案 3 :(得分:0)

我不清楚你是否要检查table2中所有行的存在性

select T1.*
from table1 as T1
where not exists (select *
                  from table2 as T2
                  where T1.variable_1 = T2.variable_1 and
                        T1.variable_2 = T2.variable_2 and
                        T1.variable_3 = T2.variable_3
                 )

或者您只想检查temp_id匹配的行。

select T1.*
from table1 as T1
  left outer join table2 as T2
    on T1.temp_id = T2.temp_id and
       T1.variable_1 = T2.variable_1 and
       T1.variable_2 = T2.variable_2 and
       T1.variable_3 = T2.variable_3
where T2.temp_id is null        

使用您的测试数据结果是相同的: http://data.stackexchange.com/stackoverflow/qt/113272/

答案 4 :(得分:0)

这个怎么样:

select * from Table1 T1 LEFT OUTER JOIN Table2 T2
on T1.temp_id = T2.temp_id
where WHERE (T1.variable_1 <> T2.variable_1 AND
T1.variable_2 <> T2.variable_2 AND
T1.variable_3 <> T2.variable_3)