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 8052849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:40:00+00:00 2026-06-05T07:40:00+00:00

For a forum i want to fetch a forumTopic with additional, linked information like

  • 0

For a forum i want to fetch a forumTopic with additional, linked information like the lastPostDate, lastPostUserName and starterUserName.

The problem arises with the lastPostUserName and starterUserName. When a forumTopic has only one linked post it seems to work correctly and both the lastPostUserName as the starterUserName are filled. When there are multiple posts linked to a topic only the starterUserName is filled and the lastPostUserName is NULL

The structure of the database is a formCategory has a number of formTopic the forumTopic has a number of forumPost and a forumPost is linked to a user.

SELECT forumTopic.*, 
        COUNT( forumPost.id ) AS postCount,  
        MAX(forumPost.date) AS lastPostDate,
        (SELECT name FROM user AS u1 WHERE u1.id = forumPost.posterUserId AND forumPost.date = MAX(forumPost.date) )
                                AS lastPostUserName,
        (SELECT name FROM user AS u2 WHERE u2.id = forumPost.posterUserId AND forumPost.date = MIN(forumPost.date) )
                                AS starterUserName

FROM forumCategory
LEFT JOIN forumTopic ON forumCategory.id = forumTopic.forumCategoryId 
LEFT JOIN forumPost ON forumPost.forumTopicId = forumTopic.id 

WHERE forumCategory.rewrittenName='someforumcategory' 
        AND forumCategory.active='Y' 
        AND forumTopic.active='Y' 
        AND forumPost.active='Y' 

GROUP BY forumTopic.id
ORDER BY forumPost.date ASC
  • 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-06-05T07:40:02+00:00Added an answer on June 5, 2026 at 7:40 am

    Try this

    SELECT forumTopic.*, 
            innerv.*, 
            (SELECT name FROM user AS u1 WHERE u1.id = innerv.first_user)
                                    AS startedUserName,
            (SELECT name FROM user AS u2 WHERE u2.id = innerv.last_user )
                                    AS lastUserName
    FROM forumTopic
    LEFT JOIN forumCategory ON forumCategory.id = forumTopic.forumCategoryId 
    LEFT JOIN (
    SELECT forumTopicId, MAX(date) AS LAST_POSTED_dATE, MIN(date) as FIRST_POSTED_DATE,
    SUBSTRING_INDEX(
    GROUP_CONCAT(posterUserId ORDER BY date),
    ',',
    1
    ) as first_user,
    SUBSTRING_INDEX(
    GROUP_CONCAT(posterUserId ORDER BY date),
    ',',
    -1
    ) as last_user, count(1) as posts_under_topic
    FROM forumPost where forumPost.active='Y' 
    GROUP BY forumTopicId ) innerv ON innerv.forumTopicId = forumTopic.id 
    WHERE forumCategory.rewrittenName='someforumcategory' 
            AND forumCategory.active='Y' 
            AND forumTopic.active='Y' 
    

    The subquery (innerv) filter active records and groups the records in the forumPost by topicId.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a basic forum set up. I want the posts#index action to only
I wanna print a incresing number with each post. Like every forum has this.
I have problem with php's preg_match.I want to make something like this : When
I've made a little forum and I want parse the date on newest posts
I want to send new thread in vbulletin forum through the application i made
I am creating a forum software. I want admins and mods to be able
At first I want to say: I couldn't find a support forum for this
I am setting up dynamic forum signature images for my users and I want
I'm building a simple web based forum application. I want to allow users to
I need to write a forum application for a friend's web site. I want

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.