Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:43:17+00:00 2026-05-27T17:43:17+00:00

I want to code a forum with subforums and need to create an overview

  • 0

I want to code a forum with subforums and need to create an overview of a subforum displaying the latest post of a thread in a html-table.

I have two mysql tables:

forum_threads:

id int(10)
forumid int(10)
title varchar(80)
accesslevel int(2)
locked enum(‘yes’, ‘no’)

forum_post:

id int(10)
threadid int(10)
userid int(10)
posted datetime
edited datetime
message text

My query looks like that:

SELECT * FROM `forum_threads` LEFT JOIN `forum_post`
ON (forum_post.threadid = forum_threads.id)
WHERE forumid='$secfid'
AND accesslevel<='$secuserlevel'
ORDER BY forum_post.posted DESC
LIMIT $qStart,$ppp

Where $secfid is the id of the subforum.

My problem ist that the result set contains all posts and because of that, each thread is contained as many times as its number of posts.
Each thread should only be there once and only with the latest post in it’s result row.

I could solve it with an additional subquery but I wanted to ask if there is a better way to do it.

I also found something like this on stackoverflow:

SELECT * FROM `forum_post` fp
JOIN `forum_threads` ft
ON (fp.threadid = ft.id) WHERE forumid=1
AND accesslevel<='100'
AND NOT EXISTS (
    SELECT * FROM forum_post fp2
    JOIN forum_threads ft2
    ON fp2.threadid = ft2.id
    WHERE ft.forumid = ft2.forumid
    AND fp.posted < fp2.posted
) LIMIT 0,20

This returns all the desired information but only from the thread with the latest post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:43:17+00:00Added an answer on May 27, 2026 at 5:43 pm

    I would do it by joining on a subselect that retrieved the latest post from each thread.

    The subselect will give you back the latest posted date in each thread. This can then be used to get thread and post data:

    SELECT
        thread.title,
        post.text
    FROM
        forum_thread thread
    
    -- Get latest post from each thread 
    JOIN (
        SELECT      
            MAX(post.posted) as posted,
            post.threadid
        FROM
            forum_post post
    
        JOIN 
            forum_thread thread
        ON 
            post.threadid = thread.id
    
        -- Limit the forum and security levels  
        WHERE
            thread.forumid = '$secfid'
            AND thread.accesslevel <= '$secuserlevel'       
        GROUP BY
            post.threadid
    
    ) latest_post
    ON 
        thread.id = latest_post.threadid
    
    -- Get post data for the latest post
    JOIN
        forum_post post
    ON
        latest_post.threadid = post.threadid
        AND latest_post.posted = post.posted
    
    LIMIT
        $qStart, $ppp;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have coded a forum and now im going to code the post a
There are times that I have a code snippet that I want to post
I want code to run whenever I create a new object. For example, see
I want some code to be triggered every second. Usually, I'd create a Timer
I want my code to automatically try multiple ways to create a database connection.
The code I want to have saved through TinyMCE is as follows: <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
I want to make a unique forum signature from my latest watched animes in
I have a WordPress blog with a forum, and I ONLY want Guests (unregistered
On my forum-based website, I have a link below every post for reporting spam
I want to create a diff between two pieces of text (code but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.