我遇到問題。我有兩個相同列的表。我想創建一個第三個表,這個表中將有兩個表中的id行,其中四個選擇的列將是相同的。SQL Server INNER與NULL值相交併相交
我解決了比較數據null與isnull的問題。
我寫了這樣的事情:
WITH cteCandidates (City, Street, HouseNumber, PostCode)
AS
(
SELECT City, Street, HouseNumber, PostCode
FROM Gymnasium
INTERSECT
SELECT City, Street, HouseNumber, PostCode
FROM PrimarySchool
)
select e.Id as 'Gymnasium',
p.Id as 'PrimarySchool'
FROM
Gymnasium AS e
Inner join cteCandidates AS c
on isnull(e.City ,'999999') = isnull(c.City ,'999999')
AND isnull(e.Street ,'999999') = isnull(c.Street ,'999999')
AND isnull(e.HouseNumber,'999999') = isnull(c.HouseNumber,'999999')
AND isnull(e.PostCode ,'999999') = isnull(c.PostCode ,'999999')
inner join PrimarySchool as p
on isnull(e.City ,'999999') = isnull(p.City ,'999999')
AND isnull(e.Street ,'999999') = isnull(p.Street ,'999999')
AND isnull(e.HouseNumber,'999999') = isnull(p.HouseNumber,'999999')
AND isnull(e.PostCode ,'999999') = isnull(p.PostCode ,'999999')
order by PrimarySchool
一切正常,只是代碼:
SELECT City, Street, HouseNumber, PostCode
FROM Gymnasium
INTERSECT
SELECT City, Street, HouseNumber, PostCode
FROM PrimarySchool
返回不同數目的行第一代碼。
我在做什麼錯?
你說得對。我在這些表中有重複的行。 Thx回答! – 2012-04-03 09:15:01