declare @t1 table(id int ,col1 char(1))
declare @t2 table(id int ,col2 char(1))insert @t2 select 1,'a'
insert @t2 select 2,'b'
insert @t2 select 3,'c'
insert @t1 select 1,'a'
insert @t1 select 2,null
select distinct * from @t1
select distinct * from @t1 where col1 in (select col2 from @t2) select distinct * from @t1 where col1 not in (select col2 from @t2)
/*id col1
----------- ----
1 a
2 NULL(所影响的行数为 2 行)id col1
----------- ----
1 a(所影响的行数为 1 行)id col1
----------- ---- (所影响的行数为 0 行)*/
declare @t2 table(id int ,col2 char(1))insert @t2 select 1,'a'
insert @t2 select 2,'b'
insert @t2 select 3,'c'
insert @t1 select 1,'a'
insert @t1 select 2,null
select distinct * from @t1
select distinct * from @t1 where col1 in (select col2 from @t2) select distinct * from @t1 where col1 not in (select col2 from @t2)
/*id col1
----------- ----
1 a
2 NULL(所影响的行数为 2 行)id col1
----------- ----
1 a(所影响的行数为 1 行)id col1
----------- ---- (所影响的行数为 0 行)*/
----------- ----
2 NULL(所影响的行数为 1 行)
非常感谢各位指点基本方向与你们说的一致
但还有有一些疑惑的地方select distinct * from table1 --295条记录
select distinct * from table1 where col1 in (select isnull(col2,'') from table2) --269条记录
select distinct * from table1 where col1 not in (select isnull(col2,'') from table2) --26条记录正常的话应该是将 isnull 加在 col1 上,但测试表明没有效果
将 isnull 加在 col2 后就正常了 请大家帮忙分析下