PDA

View Full Version : MySQL - limit per group help


batterj2
04-08-2009, 06:14 PM
Hi,

I'm trying to get the a single row per group ordered by an ID. I've narrowed it down to selecting the correct data and in the correct order but I can't work out how to get only one row per record from another title. Group by unfortunately only seems to return the first entry.

Here is my query so far:


select forums.forumTitle, forums.forumURLTitle, forums.forumDescription,
(select count(topicId) from `forums_topics` as topics where forums.forumId = topics.forumId) as topicCount,
users.userName, posts.postId, posts.postTitle, posts.postURLTitle, posts.postDate
from `forums_posts` as posts
left join `forums_topics` as topics on posts.topicId = topics.topicId
left join `users` as users on posts.userId = users.userId
left join `forums_forums` as forums on topics.forumId = forums.forumId
order by forums.forumTitle, posts.postDate desc


This returns the following:


forumTitle forumURLTitle forumDescription topicCount userName postId postTitle postURLTitle postDate
------------------------------------------------------------------------------------------------------------------------------------------------
Test Forum test-forum This is a test forum used for development purposes only 1 batterj2 2 Test Post 2 test-post-2 1239233745
Test Forum test-forum This is a test forum used for development purposes only 1 batterj2 1 Test Post test-post 1239231486


How can I modify the query so it returns the first entry per forum (in this case the first row)