选择不从内部联接返回的记录

时间:2014-01-15 11:36:21

标签: sql-server

我正在使用以下查询来连接我的两个表。

select a.*
from AllUK a
join AllCompanies b
on replace(a.Address1 + a.Postcode,' ','') = replace(b.street + b.Postcode,' ','')

我想返回AllUK表中的其余记录,这些记录不会由内部联接返回。

3 个答案:

答案 0 :(得分:1)

试试这个......

select a.*
from AllUK a
left join AllCompanies b
on replace(a.Address1 + a.Postcode,' ','') = replace(b.street + b.Postcode,' ','')
where replace(b.street + b.Postcode,' ','') is null

答案 1 :(得分:1)

这几乎是EXCEPT行动的定义:

select a.*
from AllUK a

EXCEPT 

select a.*
from AllUK a
join AllCompanies b
on replace(a.Address1 + a.Postcode,' ','') = replace(b.street + b.Postcode,' ','')

答案 2 :(得分:0)

我喜欢坚持INNOT IN命令,以便于阅读。

Select a.*
from AllUk a
where a.ID not in (
select a.id
from AllUK a
join AllCompanies b
on replace(a.Address1 + a.Postcode,' ','') = replace(b.street + b.Postcode,' ',''))