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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:09:00+00:00 2026-06-10T18:09:00+00:00

I am trying to code a forum website and I want to display a

  • 0

I am trying to code a forum website and I want to display a list of threads. Each thread should be accompanied by info about the first post (the “head” of the thread) as well as the last. My current database structure is the following:

threads table:
id   - int, PK, not NULL, auto-increment
name - varchar(255)

posts table:
id        - int, PK, not NULL, auto-increment
thread_id - FK for threads

The tables have other fields as well, but they are not relevant for the query. I am interested in querying threads and somehow JOINing with posts so that I obtain both the first and last post for each thread in a single query (with no subqueries). So far I am able to do it using multiple queries, and I have defined the first post as being:

SELECT *
FROM threads t
LEFT JOIN posts p ON t.id = p.thread_id
ORDER BY p.id
LIMIT 0, 1

The last post is pretty much the same except for ORDER BY id DESC. Now, I could select multiple threads with their first or last posts, by doing:

SELECT *
FROM threads t
LEFT JOIN posts p ON t.id = p.thread_id
ORDER BY p.id
GROUP BY t.id

But of course I can’t get both at once, since I would need to sort both ASC and DESC at the same time.
What is the solution here? Is it even possible to use a single query? Is there any way I could change the structure of my tables to facilitate this? If this is not doable, then what tips could you give me to improve the query performance in this particular situation?

  • 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-10T18:09:02+00:00Added an answer on June 10, 2026 at 6:09 pm

    You could do something with a subquery and joins:

    SELECT first.text as first_post_text, last.text as last_post_text
    FROM
      (SELECT MAX(id) as max_id, MIN(id) as min_id FROM posts WHERE thread_id = 1234) as sub
    JOIN posts first ON (sub.max_id = first.id)
    JOIN posts last ON (sub.min_id = last.id)
    

    But that doesn’t solve your problem of doing it without subqueries.

    You could add columns to your threads table so that you keep the id of the first and last post of each thread. The first post would never change, but every time you added a new post you would have to update that record in the threads table, so that would double your writes, and you may need to use a transaction to avoid race conditions.

    Or you could go so far as to duplicate information about the first and last post in the threads row. Say you needed the user_id of the poster, the timestamp it was posted, and the first 100 characters of the post. You could create 6 new columns in the threads table to contain those pieces of data for the first and last post. It duplicates data, but it means you may be able to display a list of threads without needing to query the posts table at all.

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

Sidebar

Related Questions

I have searched for two weeks, every night, exhaustively reading forum threads and trying
I'm using an iframe to display a website next to a form. I want
I'm trying to send data to a remote PHP website. I want to send
I'm trying to open the zip code lookup page on the usps website and
So I am trying to grab member profile links from a forum and display
So I have the following code I want to embed in my website, the
I am trying to pass javascript code from 1 page of my website to
I am trying to port some code form java do F# that generates a
I grabbed this code form JCarousel and just trying to understand these lines below.
I am trying to echo a result from xml into my html code form

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.