Each news can have several pictures, which are ordered by the 'o' field.
news
- id_news
- date_publish
- title
pictures
- id
- id_news
- filename
- o (order in which the pictures should be presented: 1, 2, 3...)
This query returns a list of news titles, along with the first picture for the thumbnail.
SELECT n.title, p.filename
FROM news as n
LEFT JOIN picture AS p ON (p.id_news = n.id_news AND p.o = 1)
ORDER BY date_publish DESC
The problem is that, because of the CMS, if the admin deletes the picture with o=1, the o=2 should take place in the previous query. So the question is: how do i add the filename of the first available picture in the news titles query? (it should be the picture record that has the lowest value in the field o)