2015-11-26 24 views
0

这里有2个表:如何选择没有适当的键的行?

declare @Table1 table (ID int NOT NULL PRIMARY KEY, Value int) 
declare @Table2 table (ID int NOT NULL PRIMARY KEY, Value int) 

insert into @Table1 (ID, Value) 
select 1, 100 
union all 
select 2, 101 
union all 
select 3, 103 
union all 
select 4, 104 
union all 
select 5, 105 

insert into @Table2 (ID, Value) 
select 1, 100 
union all 
select 2, 110 
union all 
select 3, 111 

我需要从第一个表中的所有行,它的价值的价值观是不是在表2。怎么做?

回答

0

这样的事情?

Select * from Table1 where value not in(select distinct value from table2) 
+0

如果在什么第二个表某些值为NULL? – tesicg

+0

这不是新问题,因为如果Value的值为NULL,则上部查询不起作用。 – tesicg

+1

本文可能有所帮助:[链接](http://stackoverflow.com/questions/2686254/how-to-select-all-records-from-one-table-that-do-not-exist-in-another-表) – Nemeas

0

SELECT T1。* FROM 表1 @ T1 LEFT JOIN表2 @ T2 ON t2.value = t1.value WHERE t2.value IS NULL

相关问题