总是根据某些条件从 Table 2 中获取 1 条记录

Always Get 1 record from Table 2 based on some condition

我有两个 table,Product 和 ProductImages。

产品 table 有 2 列:产品 ID 和名称

ProductImages table 有 4 列:ID、ProductID、ImageName 和 Primary(bit)。

Product 和 ProductImages 之间的关系是一对多的关系,因此一个产品可以有很多图像,但是对于每个产品只有一个 ProductImage 是主要的。

我需要编写一个查询来获取所有产品及其主要图像。如果产品没有主图像,则应获取 ProductId 的第一条记录。

样品产品Table

| 1  | P1 |
| 2  | P2 |
| 3  | P3 |
| 4  | P4 |

产品图片示例 Table

| 1   | 1 | P1-1 | 1
| 2   | 1 | P1-2 | 0
| 3   | 1 | P1-3 | 0
| 4   | 1 | P1-4 | 0
| 5   | 2 | P2-1 | 1
| 6   | 2 | P2-2 | 0
| 7   | 3 | P3-1 | 0
| 8   | 3 | P3-2 | 0
| 9   | 4 | P4-1 | 0
| 10  | 4 | P4-2 | 0

输出Table

| 1   | 1 | P1-1 | 1
| 5   | 2 | P2-1 | 1
| 7   | 3 | P3-1 | 0
| 9   | 4 | P4-1 | 0

我希望我澄清了我的问题。请询问是否需要进一步说明。

这有点像 "quick and dirty" 但我的作品:

SELECT  pr.ProductID, pr.Name, prim.ImageName, 1 AS IsPrimary
FROM    @product pr
        INNER JOIN @productimage prim ON pr.ProductID = prim.ProductID
WHERE   prim.[Primary] = 1
UNION ALL
SELECT  pr.ProductID, pr.Name, prim.ImageName, 0 AS IsPrimary
FROM    @product pr
        INNER JOIN
        -- Get any image for this Product MIN, MAX,...what you want
        (
            SELECT  ProductID, MIN(ImageName) AS ImageName
            FROM    @productimage
            WHERE   [Primary] = 0
            GROUP BY ProductID

        ) prim ON pr.ProductID = prim.ProductID
        LEFT JOIN
        --Primary Images:
        (
            SELECT ProductID
            FROM @productimage pri
            WHERE pri.[Primary] = 1
        ) primages ON pr.ProductID = primages.ProductID
WHERE   primages.ProductID IS NULL --there is no primary image

第一个查询是针对所有具有主图像的产品,第二个查询是针对那些没有主图像的产品。

连接较少,这看起来很整洁并且可以完成工作

select a.ProductId, ProductName, ImageName, b.ID as ImageID, b.[Primary] , b.[Primary] as IsPrimary
into a
from tProduct a 
inner join tProductImages b on a.ProductID = b.ProductID 
where b.[Primary] = 1

;WITH cte AS
(
   SELECT a.ProductId, ProductName, ImageName, b.ID as ImageID, b.[Primary] as IsPrimary ,
         ROW_NUMBER() OVER (PARTITION BY b.ProductId ORDER BY b.ID) AS rn
   from tProduct a 
    inner join tProductImages b on a.ProductID = b.ProductID 
    where b.[Primary] = 0 and a.ProductID not in (select ProductId from a)
)
SELECT ImageID, ProductId, ProductName, ImageName, IsPrimary
FROM cte WHERE rn = 1
union
select  ImageID, ProductId, ProductName, ImageName, IsPrimary
from a

drop table a

供大家参考

编辑:

我只是再次检查它,直到我意识到不需要联合查询,只有下面就足够了

;WITH cte AS
(
    SELECT a.ProductId, ProductName, ImageName, b.ID as ImageID, b.[Primary] as IsPrimary ,
         ROW_NUMBER() OVER (PARTITION BY b.ProductId ORDER BY b.[Primary] desc, b.ID) AS rn
    from tProduct a 
    inner join tProductImages b on a.ProductID = b.ProductID 
)
select * from cte where rn = 1

决定尝试使用连接和子查询的解决方案:FIDDLE

SELECT * FROM(
SELECT pi.id as ImageID ,p.id as ProductID,
pi.imagename as ImageName ,pi.primarybit as Primary_
FROM product p
JOIN
(select id, imagename,productid,primarybit
 FROM productimage 
 where primarybit = 1) pi
ON p.id = pi.productid
UNION
SELECT pi.id as ImageID ,p.id as ProductID,
pi.imagename as ImageName ,pi.primarybit as Primary_
FROM product p
JOIN productimage pi
ON p.id = pi.productid
WHERE not p.id in(
  select prod.id
  from product prod
  join productimage prodimg
  on prod.id = prodimg.productid
  where prodimg.primarybit = 1)
AND pi.id IN (
  select min(prodimg.id)
  FROM product prod
  join productimage prodimg
  on prod.id = prodimg.productid
  group by prodimg.productid
)) magictable
ORDER BY ImageID

您可以使用 row_number window 函数简单地执行此操作:

select * from Products p
join (select *, row_number()  
                 over(partition by ProductID order by ID) rn from ProductImages)pi 
      on p.ProductID = pi.ProductID and pi.rn = 1

我假设主图像 ID 将在非主图像 ID 之前。

简单的交叉应用连接就可以了

Select p.*, i.* 
from Product p 
inner join ProductImage i on p.ProductId = i.ProductId
cross apply(Select 
             ProductId, 
             MinProId = MIN(ProductImageId), 
             PrimaryProductId = MIN(case when IsPrimary=1 then ProductImageId else null end)  
           from ProductImage i  
           where i.ProductId = p.ProductId 
           group by i.ProductId )s
 where i.ProductImageId= isnull(s.PrimaryProductId, s.MinProId)