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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:11:38+00:00 2026-05-11T20:11:38+00:00

What I need done is simple… but its 3am and Im probably overlooking the

  • 0

What I need done is simple… but its 3am and Im probably overlooking the obvious.

Im coding a simple forum. One table stores the forum titles, descriptions, etc, while the other stores the posts. In the forum listing, that shows the list of all forums, I want to grab the latest post in each forum, and display the post subject, poster and post ID, and date. Simple.

The only problem is, when I join to the posts table, it joins to the first record in the table, not the last, which would denote the last post in that forum.

Here is the simplified query that gets a list of forums + data for the “latest” post (which now functions as “first post”).

SELECT forum_title, forum_id, post_subject, post_user, post_id, post_date FROM board_forums 
     LEFT JOIN board_posts 
     ON (forum_id = post_parentforum AND post_parentpost = 0) 
WHERE forum_status = 1
GROUP BY forum_id
ORDER BY forum_position

How can I fix this?

  • 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-11T20:11:38+00:00Added an answer on May 11, 2026 at 8:11 pm

    The problem you’re hitting is the classic Ambiguous GROUP BY issue. This is particular to MySQL, because other RDBMS (and standard SQL) won’t allow your query at all. Your query fails the Single-Value Rule, because you haven’t listed all non-aggregated columns in the GROUP BY.

    Here’s a solution demonstrating my favorite way of getting greatest row per group:

    SELECT f.forum_title, f.forum_id, p1.post_subject, p1.post_user, 
      p1.post_id, p1.post_date 
    FROM board_forums f
    LEFT JOIN board_posts p1
      ON (f.forum_id = p1.post_parentforum AND p1.post_parentpost = 0)
    LEFT JOIN board_posts p2
      ON (f.forum_id = p2.post_parentforum AND p2.post_parentpost = 0 
          AND p1.post_id < p2.post_id)
    WHERE p2.post_id IS NULL AND f.forum_status = 1
    ORDER BY f.forum_position;
    

    If p2.post_id IS NULL, that means no post is found in p2 which is greater than the post found in p1.

    Ergo, p1 is the latest post (assuming post_id is auto-incrementing).


    Re comment:

    Slight problem with this. post_id with the highest ID is not necessarily the latest post.

    No problem. Just use a column that is guaranteed to distinguish an earlier post from a later post. You mention post_date. In the case of ties, you’ll have to break ties with another column (or columns) that will be sure to be in chronological order.

    LEFT JOIN board_posts p2
      ON (f.forum_id = p2.post_parentforum AND p2.post_parentpost = 0 
        AND (p1.post_date < p2.post_date 
          OR p1.post_date = p2.post_date AND p1.post_millisecond < p2.post_millisecond))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display an image, which I've done without problems before, but today
I need a simple search form on my Drupal site but I can't figure
I have done some simple servlets in the past but no SOAP WS clients.
I need some simple calculations done using jquery. This example http://jsfiddle.net/tbL5r/ is pretty close.
I created a simple heatmap graph with ggplot2 but I need to force the
I'm hoping this is fairl simple but having done a lot of googling and
I need to convert some code done by someone else, to work in my
Need to know which event triggered after text selection done in Apple iPad. It
We need to redo a database in MySQL that has been already done on
I need to build a backend for an iPad app done in Objective C.

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.