I have a table Product which have many Images.
Product
id name
1 Sofa
2 Bed
Images
id product_id image_url sort_order
1 1 "url5" 521
2 1 "url1" 200
3 1 "url1" 100
4 2 "url1" 1
5 2 "url2" 2
I want to fetch images where sort_order have minimum value for 100 products like below:
id product_id image_url sort_order
3 1 "url1" 100
4 2 "url1" 1
I know I need to use min(sort_order) for images but don't find the correct syntax. I am trying the below. but no luck
select i.*
from images i
join product p on p.id = i.product_id
where p.id in (1, 2, ....)
and i.sort_order = min(sort_order)
Any help to build the correct query?