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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:31:19+00:00 2026-06-14T14:31:19+00:00

The Table: (`post_id`, `forum_id`, `topic_id`, `post_time`) (79, 8, 4, ‘2012-11-19 06:58:08’); (80, 3, 3,

  • 0

The Table:

(`post_id`, `forum_id`, `topic_id`, `post_time`) 
(79, 8, 4, '2012-11-19 06:58:08');
(80, 3, 3, '2012-11-19 06:58:42'),
(81, 9, 9, '2012-11-19 06:59:04'),
(82, 11, 6, '2012-11-19 16:05:39'),
(83, 9, 9, '2012-11-19 16:07:46'),
(84, 9, 11, '2012-11-19 16:09:33'),

The Query:

SELECT  post_id, forum_id, topic_id FROM posts 
GROUP BY topic_id 
ORDER BY post_time DESC
LIMIT 5

The Results:

[0] => [post_id] => 84 [forum_id] => 9 [topic_id] => 11  
[1] => [post_id] => 82 [forum_id] => 11 [topic_id] => 6  
[2] => [post_id] => 81 [forum_id] => 9 [topic_id] => 9  
[3] => [post_id] => 80 [forum_id] => 3 [topic_id] => 3  
[4] => [post_id] => 79 [forum_id] => 8 [topic_id] => 4

The Problem:

How to rewrite the query so that it returns post_id -> 83 instead of post_id -> 81 ?

They both have the same forum and topic ids, but post_id -> 81 has an older date than post_id -> 83.

But it seems that Group By gets the ‘first’ record and not the ‘newest’ one.

I tried changing the query to

SELECT  post_id, forum_id, topic_id, MAX(post_time)

but that returns both post_id 81 and 83

  • 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-14T14:31:20+00:00Added an answer on June 14, 2026 at 2:31 pm

    If you select attributes that are not used in the group clause, and are not aggregates, the result is unspecified. I.e you don’t know which rows the other attributes are selected from. (The sql standard does not allow such queries, but MySQL is more relaxed).

    The query should then be written e.g. as

    SELECT post_id, forum_id, topic_id
    FROM posts p
    WHERE post_time =
      (SELECT max(post_time) FROM posts p2
       WHERE p2.topic_id = p.topic_id
       AND p2.forum_id = p.forum_id)
    GROUP BY forum_id, topic_id, post_id
    ORDER BY post_time DESC
    LIMIT 5;
    

    or

    SELECT post_id, forum_id, topic_id FROM posts
    NATURAL JOIN
    (SELECT forum_id, topic_id, max(post_time) AS post_time
     FROM posts
     GROUP BY forum_id, topic_id) p
    ORDER BY post_time
    LIMIT 5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table posts and posts_votes . In posts_votes I have id and post_id
The following query takes 25 seconds with 2 table joins. The first posts table
Abstract From a table holding various posts of users to a forum, another table
I am trying to get the latest modified posts, from a group of posts,
I have a postgresql table storing posts from an online forum. Each post belongs
My table 'Post' has field: id, name, content, post_id When i create new post,
I have a mysql table which columns are post_id and post_msg. Now I want
I have the following tables: posts (post_id, content, etc) comments (comment_id, post_id, content, etc)
I have a table of forum posts, and want to improve the basic search
I have a table posts in MYSQL for some QA forum (for eg. stackoverflow),

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.