Im having difficulties creating a query. I have googled a lot but did not find a fitting solution.
I have a table called ‘blog'(parent) and a table called ‘entry'(child):
blog
- id
- subject
- owner
entry
- id
- blog_id
- published (1 = published, 0 = unpublished)
- date_created (timestamp, when created and when published)
A blog has multiple entries. Only published entries are visible to other users.
I want to create a query, which fetches the 10 latest modified blogs. For this, i would probably need a ‘foreach’ equivalent in SQL to search ALL blogs, and order them by the latest PUBLISHED entry (entry.date_modified) but only IF entry.published = 1
My SQL knowledge is not on a level which allows me to create such a query.
Additional info: using PHP (codeigniter) with MySQL
I tried this, knowing that it would never work, but it may help you as a reference for what i am trying to accomplish
SELECT blog.id
FROM blog
JOIN entry ON entry.blog_id = blog.id
WHERE entry.published = 1
ORDER by entry.date_created desc
LIMIT 0, 10
i want my end result to be the 10 blog.id’s of which their child entry was published most recenlty. Can someone please help? All help is greatly appreciated.
If this is impossible to achieve in 1 query, perhaps with PHP i can make a foreach statement performing individual queries?
I believe this would work: