日期:2014-05-18  浏览次数:20570 次

怎么找出 不重复的 急,在线等
CREATE TABLE [dbo].[Images](
[ImgID] [nvarchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ImgName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[ImgUrl] [nvarchar](150) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ImgSize] [float] NULL,
[ImgWidth] [nvarchar](4) COLLATE Chinese_PRC_CI_AS NULL,
[ImgHeight] [nvarchar](4) COLLATE Chinese_PRC_CI_AS NULL,
[ImgType] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[ImgCate] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
 CONSTRAINT [PK_Images] PRIMARY KEY CLUSTERED 
(
[ImgID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[ProductImage](
[ImgID] [nvarchar](30) COLLATE Chinese_PRC_CI_AS NOT NULL,
[ProductID] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[PageValue] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[OrderValue] [int] NULL,
 CONSTRAINT [PK_ProductPhoto] PRIMARY KEY CLUSTERED 
(
[ImgID] ASC,
[ProductID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


ProductImage的ImgID,ProductID有多条,现在需要唯一的productid,只需要images里一条imgid


------解决方案--------------------
ProductImage中存在一个productid对应多个imgid,现在是想为每个productid保留唯一一个imgid?这个唯一一个有标准吗?随便取一个?
------解决方案--------------------
declare @ProductImage table(ProductID int ,ImgID int)


insert @ProductImage select 1,1 union all select 1,2

select ProductID, max(ImgID) as ImgID from @ProductImage group by ProductID
select ProductID, min(ImgID) as ImgID from @ProductImage group by ProductID
select * from @ProductImage a where not exists(select 1 from @ProductImage where productid = a.productid and imgid >a.imgid)

------解决方案--------------------
SQL code
select
 * 
from 
ProductImage t 
where
 imgid=(select min(imgid) from @ProductImage where productid = t.productid )

------解决方案--------------------
SQL code
SELECT b.* FROM
(SELECT DISTINCT ImgID FROM images) a
CROSS APPLY
(SELECT TOP(1) * FROM [ProductImage] WHERE ImgID = a.ImgID /*order by OrderValue desc*/) b