Sql 查询错误,应该 return 只有曾经在另一个 table 中的供应商

Sql Query Error which should return only Suppliers that are once in a other table

如果我尝试执行它,我会遇到错误:

create View vwEinzellieferant
as
Select P.SupplierID, S.CompanyName, S.ContactName, S.Address + ' ' + S.City + ' ' + S.Region + ' ' + S.PostalCode + ' ' + S.Country, S.Phone
from Suppliers S inner join Products P on S.SupplierID = P.SupplierID
group by P.SupplierID, S.CompanyName, S.ContactName, S.Address, S.City, S.Region, S.PostalCode, S.Country, S.Phone
having (Count(S.SupplierID in (Select SupplierID from Products))) > 2;

在有一个问题,但我不知道是什么。 解释:查询应该创建一个视图,但只包含曾经出现在产品列表中的供应商。 有人可以帮助我吗?

似乎您需要重新排列 HAVING 子句,以便 return 相关的 COUNT 值等于 1,例如

SELECT P.SupplierID,
       S.CompanyName,
       S.ContactName,
       S.Address + ' ' + S.City + ' ' + S.Region + ' ' + S.PostalCode + ' ' +
       S.Country,
       S.Phone
  FROM Suppliers S
  JOIN Products P
    ON S.SupplierID = P.SupplierID
 GROUP BY P.SupplierID,
          S.CompanyName,
          S.ContactName,
          S.Address,
          S.City,
          S.Region,
          S.PostalCode,
          S.Country,
          S.Phone
 HAVING ( SELECT COUNT(*) FROM Products WHERE SupplierID = S.ID ) = 1