Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Query Limiting Problems
Old 12-02-2007, 06:13 PM Query Limiting Problems
Stieffers's Avatar
Novice Talker

Posts: 7
Name: Taylor S
Trades: 0
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.
Stieffers is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-03-2007, 04:46 PM Re: Query Limiting Problems
SmartBomb's Avatar
Average Talker

Posts: 27
Name: Dave
Trades: 0
I think you could use the "GROUP BY" on the thread and "HAVING" clause to accomplish what you're trying to do without using the subquery.
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
SmartBomb is offline
Reply With Quote
View Public Profile
 
Old 12-03-2007, 08:25 PM Re: Query Limiting Problems
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
SmartBomb is probably right. Group By Thread Having Count(*) > 1
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 12-03-2007, 10:17 PM Re: Query Limiting Problems
Stieffers's Avatar
Novice Talker

Posts: 7
Name: Taylor S
Trades: 0
I tried grouping by thread.threadId and then using the "HAVE" clause and count function but that only returns threads with more than one post. Not the effect I was going after.
Stieffers is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Query Limiting Problems
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13541 seconds with 12 queries