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

  • Home
  • SEARCH
  • 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 493707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:25:51+00:00 2026-05-13T05:25:51+00:00

I am trying to create SQL for retrieveing a list of latests posts for

  • 0

I am trying to create SQL for retrieveing a list of latests posts for the forum thread.
I have the following code:

SELECT
 item_discuss_thread_id
 , item_discuss_post_title
 , COUNT(item_discuss_thread_id) AS nb_posts
FROM
 item_discuss_posts
GROUP BY
 item_discuss_thread_id

Obviously this will group without the respect of if the post is latest or not. item_discuss_post_title will just get the first row in the group.

I wonder if there’s some way around this? If not, what is the best way to solve the problem… only subqueries?

Thanks,
Pavel

UPDATE:
Please note that I need all threads, LIMIT 1 is not solving the problem.
Also ORDER BY is not an option as GROUP BY will select the first record from group anyway.
This is not such a simple question as it can seem to be.

UPDATE:

I really want to try to avoid using subqueries or if doing so – use it the optimal may.
What I came with currently is something like this:

SELECT
  ordered_by_date.item_discuss_thread_id
  , item_discuss_post_title
  , COUNT(item_discuss_thread_id) AS nb_posts
FROM
  (
   SELECT
     item_discuss_thread_id
     , item_discuss_post_title
   FROM
      item_discuss_posts
   ORDER BY
      item_discuss_post_datetime DESC
  ) AS ordered_by_date
GROUP BY
   item_discuss_thread_id

EXPLAIN EXTENDED gives the following result:

id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, PRIMARY, <derived2>, ALL, \N, \N, \N, \N, 20, Using temporary; Using filesort
2, DERIVED, item_discuss_posts, index, \N, item_discuss_post_datetime, 8, \N, 20, 
  • 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-13T05:25:52+00:00Added an answer on May 13, 2026 at 5:25 am

    Try this.

    SELECT
      *
    FROM
      (SELECT item_discuss_thread_id, item_discuss_post_title, COUNT(item_discuss_thread_id) AS nb_posts
       FROM item_discuss_posts
       ORDER BY __datecolumn__)
      AS ordered_by_date
    GROUP BY
      ordered_by_date.item_discuss_thread_id
    

    Replace __datecolumn__ with the column that stores posting time.

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

Sidebar

Related Questions

I'm trying to create a new function in SQL with the following code: CREATE
I am trying to create some dynamic sql using the following code block firstSqlStatement
I have the following table: I am trying to create an SQL query that
I'm currently trying to create a T-SQL, which runs through a list of deliveries
I am trying to create an SQL database in Android. I am currently following
I am trying to create a function in MySQL: Here is the SQL code:
I am trying to create a products table running the following SQL query: CREATE
I am trying to create SQL Select that returns counts of a certain field
I am trying to create a Sql function,Where a list of students is fetched
I'm trying to create the following SQL query (as in, this is an example

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.