0

SQL 表中的产品表:

Product: 
    ID,
    Name

ProductImage
    ID,
    ProductID,
    Image

我想在产品的选择查询中选择一个图像我需要 product1 的第一张/最后一张图像、product2 的第一张/最后一张图像等

就像是:

select Product.id,Product.name,(select top(1)image from productimage where productimage.ProductID=product.ID)as Image from product
4

2 回答 2

0

试试这个,也许有用:

select Product.id,Product.name,
(select top (1) image from productimage where productimage.ProductID=product.ID order by productimage.ID asc)as FirstImage ,
(select top (1) image from productimage where productimage.ProductID=product.ID order by productimage.ID desc) as LastImage
from product
于 2013-05-15T13:29:50.137 回答
0

临时表子查询更适合在列级子查询上长期运行。

前任:

SELECT
    Product.id,
    Product.name,
    productImageTable.Image AS Image 
FROM Product INNER JOIN
(SELECT 
    ProductID,
    Min(image) as Image 
FROM productimage 
GROUP BY ProductID) AS productImageTable ON 
    productImageTable.ProductID=Product.ID
于 2019-11-06T10:14:57.237 回答