SQL 发现不匹配的加入
SQL Join that finds non matches
我有两个表“SellerDetails”和“ANL”,它们的关键字段都是“Seller Ref No”和“Sales Year”。
这让我找到销售年份为“20”且部门为“远程销售”的卖家参考号匹配项
SELECT si.[Seller Ref No] FROM dbo.SellerDetails AS si, dbo.ANL AS a
WHERE si.[Seller Ref No] = a.[Seller Ref No]
AND si.Department = 'Remote Sales'
AND si.[Sales Year] = '20';
但是我如何找出“SellerDetails”中的记录,而不是在“ANL”中的记录,其中销售年份为“20”且部门为“远程销售” “?我已经试过了,但它 returns 的数量比我预期的要大得多。我也尝试过将“=”更改为“<>”,但结果更糟
SELECT si.[Seller Ref No]
FROM dbo.SellerDetails AS si
LEFT JOIN dbo.ANL AS a ON (si.[Seller Ref No] = a.[Seller Ref No])
WHERE si.Department = 'Remote sales'
AND si.[Sales Year] = '20';
您可以使用 not exists
:
select s.*
from dbo.SellerDetails s
where
s.Department = 'Remote Sales'
and s.[Sales Year] = 20
and not exists (select 1 from dbo.ANL a where a.[Seller Ref No] = s.[Seller Ref No])
不清楚您是否也想使用 [Sales Year]
作为关联条件:它存在于两个表中,但您的查询不使用该关系。如果你想要,那么:
select s.*
from dbo.SellerDetails s
where
s.Department = 'Remote Sales'
and s.[Sales Year] = 20
and not exists (select 1 from dbo.ANL a where a.[Seller Ref No] = s.[Seller Ref No] and a.[Sales Year] = s.[Sales Year])
旁注:这是您可能想要使用 left join
:
编写的查询
SELECT si.[Seller Ref No]
FROM dbo.SellerDetails AS si
LEFT JOIN dbo.ANL AS a ON si.[Seller Ref No] = a.[Seller Ref No]
WHERE
si.Department = 'Remote sales'
AND si.[Sales Year] = '20'
AND a.[Seller Ref No] IS NULL
WHERE
子句中的最后一个条件过滤掉匹配的行。
SELECT si.[Seller Ref No]
FROM dbo.[SellerDetails] AS si
LEFT JOIN dbo.[ANL] AS a ON si.[Seller Ref No] = a.[Seller Ref No]
AND si.[Department] = 'Remote sales'
AND si.[Sales Year] = '20'
Where a.[Seller Ref No] is null
我有两个表“SellerDetails”和“ANL”,它们的关键字段都是“Seller Ref No”和“Sales Year”。
这让我找到销售年份为“20”且部门为“远程销售”的卖家参考号匹配项
SELECT si.[Seller Ref No] FROM dbo.SellerDetails AS si, dbo.ANL AS a
WHERE si.[Seller Ref No] = a.[Seller Ref No]
AND si.Department = 'Remote Sales'
AND si.[Sales Year] = '20';
但是我如何找出“SellerDetails”中的记录,而不是在“ANL”中的记录,其中销售年份为“20”且部门为“远程销售” “?我已经试过了,但它 returns 的数量比我预期的要大得多。我也尝试过将“=”更改为“<>”,但结果更糟
SELECT si.[Seller Ref No]
FROM dbo.SellerDetails AS si
LEFT JOIN dbo.ANL AS a ON (si.[Seller Ref No] = a.[Seller Ref No])
WHERE si.Department = 'Remote sales'
AND si.[Sales Year] = '20';
您可以使用 not exists
:
select s.*
from dbo.SellerDetails s
where
s.Department = 'Remote Sales'
and s.[Sales Year] = 20
and not exists (select 1 from dbo.ANL a where a.[Seller Ref No] = s.[Seller Ref No])
不清楚您是否也想使用 [Sales Year]
作为关联条件:它存在于两个表中,但您的查询不使用该关系。如果你想要,那么:
select s.*
from dbo.SellerDetails s
where
s.Department = 'Remote Sales'
and s.[Sales Year] = 20
and not exists (select 1 from dbo.ANL a where a.[Seller Ref No] = s.[Seller Ref No] and a.[Sales Year] = s.[Sales Year])
旁注:这是您可能想要使用 left join
:
SELECT si.[Seller Ref No]
FROM dbo.SellerDetails AS si
LEFT JOIN dbo.ANL AS a ON si.[Seller Ref No] = a.[Seller Ref No]
WHERE
si.Department = 'Remote sales'
AND si.[Sales Year] = '20'
AND a.[Seller Ref No] IS NULL
WHERE
子句中的最后一个条件过滤掉匹配的行。
SELECT si.[Seller Ref No]
FROM dbo.[SellerDetails] AS si
LEFT JOIN dbo.[ANL] AS a ON si.[Seller Ref No] = a.[Seller Ref No]
AND si.[Department] = 'Remote sales'
AND si.[Sales Year] = '20'
Where a.[Seller Ref No] is null