0

product table in SQL tables:

Product: 
    ID,
    Name

ProductImage
    ID,
    ProductID,
    Image

I want to select an image in select query of Product I need first/last image of product1, first/last image of product2, etc

Something like:

select Product.id,Product.name,(select top(1)image from productimage where productimage.ProductID=product.ID)as Image from product
VISHMAY
  • 699
  • 5
  • 20
  • 1
    possible duplicate of [Get top 1 row of each group](http://stackoverflow.com/questions/6841605/get-top-1-row-of-each-group) – gbn May 15 '13 at 13:13

2 Answers2

0

Try this,Maybe useful:

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
Fuad
  • 231
  • 2
  • 10
0

Temp Table subqueries are better for long run over column level subqueries.

Ex:

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
VISHMAY
  • 699
  • 5
  • 20