mysql - SQL query to bring linked record with lowest value in field -


each news can have several pictures, ordered 'o' field.

news

  • id_news
  • date_publish
  • title

pictures

  • id
  • id_news
  • filename
  • o (order in pictures should presented: 1, 2, 3...)

this query returns list of news titles, along first picture thumbnail.

select n.title, p.filename news n left join picture p on (p.id_news = n.id_news , p.o = 1) order date_publish desc 

the problem that, because of cms, if admin deletes picture o=1, o=2 should take place in previous query. question is: how add filename of first available picture in news titles query? (it should picture record has lowest value in field o)

how adding different join here?

select n.title, p.filename news n left join picture p on  (p.id_news = n.id_news) inner join (select id_news,min(o) o picture) p1 on (p1.id_news = n.id_news , p1.o = p.o) order n.date_publish desc 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -