3

I am working on a simple forum in symfony2 and have the following DQL query to get the last posts in each forum:

SELECT p, f.id FROM MyBundle:Post p
JOIN p.forum f
GROUP BY p.forum
HAVING p.created_at >= MAX(p.created_at)

Without the HAVING clause it works and fetches the first posts but thats not what I want.

What am I doing wrong here? Is this even possible using DQL?

maiwald
  • 851
  • 12
  • 26

1 Answers1

3

Of course it was my fault and not DQL's. :) The updated query is a bit more complex and requires a Subquery. If Someone has a better way to do this, please let me know.

SELECT post, f.id as forum_id FROM MyBundle:Post post
JOIN post.forum f
WHERE post.id in (
    SELECT MAX(p.id) FROM MyBundle:Post p
    GROUP BY p.forum
)

This Blog Post completed my fading sql knowledge.

maiwald
  • 851
  • 12
  • 26