简体   繁体   English

我试图从 3 个(或更多)不同的行中生成一行,只显示一个图像

[英]I'm trying to result one row from 3 (or more) different rows with only one image to display

I'm trying to result one row from 3 (or more) different rows with only one image to display and IsOriginal = 1 based on the same barcode (or id).我试图从 3 个(或更多)不同的行中生成一行,只显示一个图像,并且 IsOriginal = 1 基于相同的条形码(或 id)。

Here is my table for example:这是我的表,例如:

Products产品

---------------------------------------------------------------
|  id  | IsOriginal |    Name    |    Brcode    |    Image    |
---------------------------------------------------------------
|  1   |     0      |   Pasta    |  123456789   |  pasta1.jpg |
---------------------------------------------------------------
|  1   |     0      |   Pasta    |  123456789   |  pasta2.jpg |
---------------------------------------------------------------
|  1   |     1      |   Pasta    |      1       |     NULL    |
---------------------------------------------------------------

I"m trying to result this:我试图导致这个:

---------------------------------------------------------------
|  id  | IsOriginal |    Name    |    Brcode    |    Image    |
---------------------------------------------------------------
|  1   |     1      |   Pasta    |      1       |  pasta1.jpg |
---------------------------------------------------------------

Here is my code:这是我的代码:

SELECT 
    p1.id, 
    p1.IsOriginal, 
    p1.Name, 
    p1.Brcode, 
    p2.Image as Image
FROM Products p1
join Products p2
on p1.id = p2.id
where p1.IsOriginal = 1

and p2.Image is not null并且 p2.Image 不是 null

Is this what you want?这是你想要的吗?

select p.*,
       (select p2.image
        from products p2
        where p2.id = p.id and p2.image is not null
        fetch first 1 row only
       ) as imputed_image
from products p
where isOriginal = 1;

This returns an arbitrary matching image for the same id.这将返回相同 id 的任意匹配图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM