So, I have this addon on my vBulletin forums that shows the five latest posts. The problem is that it shows the five latest posts regardless of them being in the same thread. So at times I'll get a list of five posts all my in the same thread. This sort of defeats the purpose of the addon, and I'm trying to limit the query to only display results from unique threads. Here's what I've got so far.
Code:
// Crazy query! Match threadid from vb_post, then forumid from vb_thread.
// Exclude forums that the user can't view.
// Limit from settings.
// SELECT alltheinfo FROM allthetables WHERE joins AND threadid IN [subquery]
$latestthreads = $db->query_read("
SELECT post.threadid AS p_threadid, post.username AS p_username, post.userid AS p_userid, post.postid AS p_postid,
thread.*,
forum.forumid AS f_forumid, forum.title AS f_title
FROM " . TABLE_PREFIX . "post post,
" . TABLE_PREFIX . "thread thread,
" . TABLE_PREFIX . "forum forum
WHERE open='1'
$excludedthreads
AND thread.threadid = post.threadid
AND forum.forumid = thread.forumid
AND thread.threadid IN (
SELECT thread.threadid
FROM " . TABLE_PREFIX . "thread thread
ORDER BY thread.lastpostid DESC
)
ORDER BY post.postid DESC
LIMIT 0, $number
");
As you can see I'm using a subquery to try and achieve the desire result set, but it doesn't seem to be working. I'm not quite sure where to go with it at this point and Sql isn't my strong point. Any help would be appreciated.
|