7

我在 SQL Server 2008 上使用以下 SQL 查询从表products中选择行。categories

SELECT products.idProduct,  sku, description, listPrice, 
   smallImageUrl, isBundleMain, rental, visits 
FROM products, categories_products 
WHERE products.idProduct = categories_products.idProduct 
AND categories_products.idCategory = "& pIdCategory&" 
AND listHidden=0 
AND active=-1 
AND idStore = " &pIdStore& "
ORDER BY description

问题是有些行是重复的。这些重复项通常由products.idProduct列确定,因此我想更改查询以使相同的查询products.idProduct不会出现两次,例如,其中一行具有products.idProduct = 3438,另一行具有相同的产品 ID,并且仅products.idProduct显示其中一个

4

3 回答 3

9

你需要使用不同的。试试下面

SELECT distinct 
  products.idProduct, sku, description, listPrice, smallImageUrl, 
  isBundleMain, rental, visits 
FROM products, categories_products 
WHERE products.idProduct=categories_products.idProduct 
  AND categories_products.idCategory="& pIdCategory&" 
  AND listHidden=0 AND active=-1 
  AND idStore=" &pIdStore& "  
ORDER BY description
于 2013-03-13T18:47:45.100 回答
3

DISTINCT如下图使用:

SELECT DISTINCT products.idProduct, 
       sku, description, listPrice, 
       smallImageUrl, isBundleMain, rental, visits 
FROM products, categories_products 
WHERE products.idProduct = categories_products.idProduct 
AND categories_products.idCategory = "& pIdCategory&" 
AND listHidden = 0 AND active = -1 
AND idStore =" &pIdStore& "  
ORDER BY description
于 2013-03-13T18:49:35.390 回答
3

在字段的选择查询中使用 DISTINCT。

于 2013-03-13T18:51:05.067 回答