I have two tables, Article and Image with one-to-many where Article is the parent and the Image has foreign key column article_id
. Each Image also has the column position
that is integer
.
Now what I need is to I get all of the articles, regardless if there are any images for all of them, but for those that have one ore more images it needs to return a single image which has the lowest position stored in position
column.
In other words along the pseudo lines:
SELECT FROM ALL ARTICLES LEFT JOIN IMAGES (WHERE IMAGE FOR THIS ARTICLE HAS SMALLEST POSITION)
What SQL query to get the above?