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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:32:21+00:00 2026-06-18T04:32:21+00:00

Imagine I have a database with following relational schema: forums(id, title) forum_topics(id, forum_id, title,

  • 0

Imagine I have a database with following relational schema:

forums(id, title)
forum_topics(id, forum_id, title, count_views, some_data)
forum_posts(id, topic_id, title, content, author_id, another_data)

And say I have two rows in the forums_posts table with the data:

(1, 1, 'some title', 'some content', 4, 'blahr')
(2, 1, 'another post title', 'my content', 5, 'nah')

Now, I want to create a SQL Statement which will give me the topic id, count of posts for that topic AND the user id of the latest contributor to the topic. It’s obviously not a problem to get the first two values while the latter is pretty tricky. Here’s what I have so far:

SELECT topic_id,
   COUNT(*) AS count_posts,
   forum_posts.author_id AS last_answer_by
FROM forum_posts
JOIN forum_topics ON forum_posts.topic_id = forum_topics.id
GROUP BY topic_id

The query given above will give me, assumed there is a forum_topics entry with id = 1:

topic_id = 1
count_posts = 2
last_answer_by = 4

While assuming a higher post id means it has been written later than a post entry with a lower id what I want to get is:

topic_id = 1
count_posts = 2
last_answer_by = 5
  • 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-18T04:32:22+00:00Added an answer on June 18, 2026 at 4:32 am

    You only need to query against the forum_posts table to get the results you want but you need to use a subquery in your from clause to first obtain the count of the number of posts for each topic_id in the forum_posts table along with the maximum post id for the topic. You then join the results of that subquery back to the original forum_posts table on that maximum post id:

    SELECT postcounts.topic_id,
           count_posts,
           author_id as last_answer_by
    FROM
      (SELECT topic_id,
              COUNT(*) AS count_posts,
              MAX(id) AS lastpost
       FROM forum_posts
       GROUP BY topic_id) postcounts
    INNER JOIN forum_posts ON lastpost = forum_posts.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have a PostGIS database with following pieces of information OSM (OpenStreetMap) of
Imagine the following problem: You have a database containing about 20,000 texts in a
Imagine the following (simplified) database layout: We have many holiday records that relate to
Imagine we have two tables in the database, user (FK id_role) and role (PK
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I have a SQL table with the following columns and values: +----+---------+------------+---------+------------+ |
I have the following generic schema to represent different types of information. var Record
Let's imagine I have two threads which execute some database-oriented code in thread-specific TransactionScopes
Imagine a following use case: I have a dictionary of report templates. Each report

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.