SQL查找存在于某些表中且不存在的数据';不存在于另一个表中
在Microsoft SQL Server中, 我想选择表A、C、D中存在而B中不存在的数据。我可以这样写吗SQL查找存在于某些表中且不存在的数据';不存在于另一个表中,sql,sql-server,Sql,Sql Server,在Microsoft SQL Server中, 我想选择表A、C、D中存在而B中不存在的数据。我可以这样写吗 Select A.Store,C.Item,D.Cost from A Inner Join C on A.Store=C.Store and A.Item=C.Item Inner join D on C.Store=D.Store and C.Item=D.Item And Not exists (Select * from B where A.Store=B.
Select A.Store,C.Item,D.Cost
from A
Inner Join C on A.Store=C.Store and A.Item=C.Item
Inner join D on C.Store=D.Store and C.Item=D.Item
And Not exists (Select * from B where A.Store=B.Store and A.Item=B.Item)`
是的,你的问题完全有道理 您还可以使用
左连接
,如下所示:
Select A.Store,C.Item,D.Cost
from A
Inner Join C on A.Store=C.Store and A.Item=C.Item
Inner join D on C.Store=D.Store and C.Item=D.Item
Left join B on A.Store=B.Store and A.Item=B.Item
Where b.store is null;
那么你的问题出在哪里了???这似乎是正确的。有时我编写的查询会产生很多性能问题。只是看看这是不是最好的方法。