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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:25:51+00:00 2026-06-04T00:25:51+00:00

Nijas I’m having a problem with a query for receiving data to generate a

  • 0

Nijas

I’m having a problem with a query for receiving data to generate a classic forum index with all it’s information, you something like phpBB.

My tables looks like this:

categories:
  gategory varchar(50)  -> primary key

forums:
  id int -> primary key
  name varchar(255)
  description text
  category varchar(50)  -> foreign key to category

topics:
  id int -> primary key
  forum_id int -> foreign key to forums
  subject varchar(255)

posts:
  id int -> primary key
  topic_id int -> foreign key to topics
  user_id int -> foreign key to users
  post text
  create_date datetime
  modify_date timestramp, on_update(current_time)

users:
  id int -> primary key
  username varchar(32)
  password varchar(32)

And that is just great easy peasy.
Then I began building the query, and it got really complex (in my world) pretty fast.
I would like to get:

catories:
  forums:
    name,
    description,
    count(topics)
    count(posts)
      last_post user_id
      last_post username
      last_post create_date

I ended up with a working query looking like this:

SELECT
  f.id              as fid,
  f.name            as name,
  f.description     as description,
  f.category        as category,
  ( SELECT COUNT(*)
    FROM forum_topics
    WHERE forum_id = f.id
  )                 as topics,
  ( SELECT COUNT(*)
    FROM forum_posts fp
    WHERE fp.topic_id IN (
      SELECT id
      FROM forum_topics
      WHERE forum_id = f.id
    )
  )                 as posts,
  lp.user_id        as lp_userid,
  u.username        as lp_username,
  lp.create_date    as lp_date
FROM forums f
LEFT OUTER JOIN (
  SELECT p.create_date, p.user_id, t.forum_id
  FROM forum_topics t
  INNER JOIN forum_posts p ON ( t.id = p.topic_id )
  ORDER BY p.create_date DESC
) lp ON (lp.forum_id = f.id)
LEFT OUTER JOIN users u ON ( u.id = lp.user_id )
GROUP BY category, f.order

It’s fine; it works, but it performs very badly.
So I was wondering some of you clever folks at this place,
could give me some advice on how to optimize the query,
maybe put in some indices some smart places, or reconstruct the schema in a smarter way.

// Thank you very much in advance

  • 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-04T00:25:53+00:00Added an answer on June 4, 2026 at 12:25 am

    The basic query is to join all the tables together along their nature dimensions. This gets you everything except for the last post.

    The following query uses standard SQL and should work in both mysql and SQL Server (except for typos).

    SELECT
      f.category,
      f.id,
      f.name,
      f.description,
        count(distinct t.id) AS topics,
        count(distinct p.id) AS posts,
        min(lastuser.id),
        min(lastuser.username),
        min(p.create_date)
    FROM posts p
    JOIN users u ON p.user_id = u.id
    JOIN topics t ON p.topic_id = t.id
    JOIN forums f ON t.forum_id = f.id
    JOIN (SELECT
            t.forum_id,
            u.id,
            u.username,
            p.create_date
            FROM posts p
            JOIN topics t ON p.topic_id = t.id
            JOIN users u ON p.user_id = u.id
            JOIN (SELECT
                   t.forum_id, max(p.id) AS max_postid
                  FROM posts p
                  JOIN topics t ON p.topic_id = t.id
                  GROUP BY t.forum_id
                  ) lastpost ON p.id = lastpost.max_postid
                 AND t.forum_id = lastpost.forum_id
           ) lastuser on lastuser.forum_id = f.id
    GROUP BY f.category, f.id, f.name, f.description
    

    It gets the last user by another complicated set of joins. The query assumes that the posts are assigned monotonically, so the most recent post has the highest post id.

    There are other approaches. In particular, SQL Server supports window functions, which would simplify the query.

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

Sidebar

Related Questions

I have Kohana ORM/Mysql query problem. I hope you can help. To start of,
We have a simple domain where FirstName_s:Bob returns 40 documents, and -Department_t:Ninjas returns all
Let's say I have a model like this: create_table :ninjas do |t| t.string name
I'm attempting to write a TinyURL like clone in ASP.NET MVC as a first
I'm writing an interactive fiction game in java from scratch. I'm currently storing all
I'm pretty new to classic asp and working with arrays is not fun :(
I have a listbox that uses a data template. The template is very simple
Take the following LINQ query as an example. Please don't comment on the code
I have for example, the following data in my database table... 30 Miles DVD
I'm curious how the Python Ninjas around here would do the following, elegantly and

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.