我正在寻找一个查询,需要显示供应商表中没有类别 1 (Products.CategoryID = 1) 的产品的所有供应商。每当我运行它时,它总是会出错。
Select SupplierID From Suppliers su
where SupplierID NOT IN
(select distinct SupplierID from Products
where SupplierID in
(select SupplierID from Products where CategoryID=1)
附带问题:如果供应商拥有 cat 产品,我如何获得这些结果? 6? (所以没有来自 cat1,但有来自 cat6)。
不要使用子选择,而是尝试使用基于集合的操作,例如join
s or exists
。下面是适合您情况的一种选择,但有多种方法可以实现您想要做的事情。哪一个最好取决于您的数据:
select su.SupplierID
From Suppliers as su
where not exists(select null -- The only check here is for a record, so you can select any value and it won't change the functionality
from Products as p
where su.SupplierID = p.SupplierID
and p.CategoryID = 1
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)