从查询中显示不同的行记录
Show distinct row records out from query
我有两个表:
产品:
图片:
我在查询中通过 idProduct 连接这些表。
我已经形成了具有某些必要条件的查询:
select p.prdColor prdColor, p.sku,I.Image,p.descriptselect p.prdColor
prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from
products p,Images I where p.idProduct=i.idproduct and
p.PrdParentSku=(select PrdParentSku from products where
sku='120PBOOTCAT12') and I.DisplayOrder=1 and
isnull(p.prdColor,'')!=''
它给了我结果:
现在,我想通过distinct prdColor获取记录
表示prd颜色只能是cat和dinosor(上面的例子只有两条记录)
如何编写查询???
我试过了:
select distinct p.prdColor prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from
products p,Images I
where p.idProduct=i.idproduct
and p.PrdParentSku=(select PrdParentSku from products where sku='120PBOOTCAT12') and
I.DisplayOrder=1
and isnull(p.prdColor,'')!=''
group by p.sku,I.Image,p.description,ISNULL(price,0),p.prdColor
但这并没有帮助。
请帮帮我。
预计:
prdColor SKU Image Description
cat whatever whatever whatever
dianosor whatever whatever whatever
注意:- 该 prdcolor 的前 1 条记录是什么
您需要使用加入
select p.prdColor prdColor, p.sku,I.Image,p.descriptselect p.prdColor prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from products p inner join Images I ON p.idProduct=i.idproduct AND p.PrdParentSku=(select PrdParentSku from products where sku='120PBOOTCAT12') and I.DisplayOrder=1 and isnull(p.prdColor,'')!=''
像这样
尝试使用 ROW_NUMBER()
SELECT * FROM
(
select p.prdColor prdColor, p.sku,
I.Image,p.descriptselect p.prdColor
prdColor, p.sku,I.Image,
p.description,ISNULL(price,0) price,
ROW_NUMBER() OVER(partition by p.prdColor order by p.prdColor) RowNum
from products p,Images I
where p.idProduct=i.idproduct and
p.PrdParentSku=(select PrdParentSku from products where
sku='120PBOOTCAT12') and I.DisplayOrder=1 and
isnull(p.prdColor,'')!=''
) AS A
WHERE A.RowNum = 1
我有两个表:
产品:
图片:
我在查询中通过 idProduct 连接这些表。
我已经形成了具有某些必要条件的查询:
select p.prdColor prdColor, p.sku,I.Image,p.descriptselect p.prdColor
prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from
products p,Images I where p.idProduct=i.idproduct and
p.PrdParentSku=(select PrdParentSku from products where
sku='120PBOOTCAT12') and I.DisplayOrder=1 and
isnull(p.prdColor,'')!=''
它给了我结果:
现在,我想通过distinct prdColor获取记录
表示prd颜色只能是cat和dinosor(上面的例子只有两条记录)
如何编写查询???
我试过了:
select distinct p.prdColor prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from
products p,Images I
where p.idProduct=i.idproduct
and p.PrdParentSku=(select PrdParentSku from products where sku='120PBOOTCAT12') and
I.DisplayOrder=1
and isnull(p.prdColor,'')!=''
group by p.sku,I.Image,p.description,ISNULL(price,0),p.prdColor
但这并没有帮助。
请帮帮我。
预计:
prdColor SKU Image Description
cat whatever whatever whatever
dianosor whatever whatever whatever
注意:- 该 prdcolor 的前 1 条记录是什么
您需要使用加入
select p.prdColor prdColor, p.sku,I.Image,p.descriptselect p.prdColor prdColor, p.sku,I.Image,p.description,ISNULL(price,0) price from products p inner join Images I ON p.idProduct=i.idproduct AND p.PrdParentSku=(select PrdParentSku from products where sku='120PBOOTCAT12') and I.DisplayOrder=1 and isnull(p.prdColor,'')!=''
像这样
尝试使用 ROW_NUMBER()
SELECT * FROM
(
select p.prdColor prdColor, p.sku,
I.Image,p.descriptselect p.prdColor
prdColor, p.sku,I.Image,
p.description,ISNULL(price,0) price,
ROW_NUMBER() OVER(partition by p.prdColor order by p.prdColor) RowNum
from products p,Images I
where p.idProduct=i.idproduct and
p.PrdParentSku=(select PrdParentSku from products where
sku='120PBOOTCAT12') and I.DisplayOrder=1 and
isnull(p.prdColor,'')!=''
) AS A
WHERE A.RowNum = 1