<p>Here's a simplified version of my scenario:</p>

<ul>
<li>I have a table named <strong>Project</strong> that I reference via an 'id' field.</li>
<li>I have a table named <strong>Photo</strong> that has a field named 'project_id' which I use to relate multiple photos to a single project. The Photo table also has an auto-incrementing 'id' field that I use for ordering, amongst other things.</li>
</ul>

<p>Here's what I want to accomplish: <em>For a collection of Project id values, I wish to retrieve the last 5 photos added to each project</em> -- ideally in a single query, of course. :-)</p>

<p>Put another way, instead of imposing a single query limit, I would ideally like to specify a per project limit on the number of photos returned.</p>

<p>I am currently implementing this as one-query per project, so N projects = N queries (a good caching strategy will certainly reduce the hurt, but that'll come later).</p>

<p>Anyone have a solution?</p>

<p>Thanks.</p>

For the "last n items" problem in MySQL, take look here:
How to select maximum 3 items per users in MySQL?
(it's right in the top answer).
When you take it from there, all you are missing is a JOIN with your
```
Projects
```
table, which should be easy to do.