我已经在sql server 2005中编写了这个查询,但它仍然会显示相同的产品名称!!谢谢
SELECT DISTINCT ProductName
FROM Products,Suppliers
WHERE Products.SupplierID = Suppliers.SupplierID AND Fax IS NULL
例如,我有两个产品名称相同'Chei'而不是返回一个'chei'它将返回它们两个
答案 0 :(得分:1)
您发布的内容应该可以正常使用。
你有可能选择其他类似的东西吗?
SELECT DISTINCT ProductName, ProductId
FROM Products,Suppliers
WHERE Products.SupplierID = Suppliers.SupplierID AND Fax IS NULL
或者,您应该这样写(但如果您选择多列,这对您没有帮助。)
SELECT DISTINCT ProductName
FROM Products
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE Fax IS NULL
如果您对为什么选择多列不起作用感到困惑:
SELECT DISTINCT ProductName, ProductId
FROM Products
INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE Fax IS NULL
请考虑一下如下:
Products table:
ID Name
1 Test
2 Test
3 Other
4 Random
你想:
Mixed:
ID Name
4 Random
3 Other
? Test
如何在Distinct混合表中为Test选择'id'? 因此,它为您提供了所有要求的数据的所有DISTINCT组合。
即
Mixed:
ID Name
4 Random
3 Other
2 Test
1 Test
答案 1 :(得分:1)
选项:
SELECT --DISTINCT if you need RTRIM
P.ProductName --RTRIM(ProductName, '')
FROM
Products P
WHERE
EXISTS (SELECT *
FROM
Suppliers S
WHERE
P.SupplierID = S.SupplierID
AND
Fax IS NULL -- belongs to Suppliers?
)