Give it a whirlwind and see if it fits what you want. This should show you the last 100 posts, and the results will be returned sorted by the stream to which they belong.
SELECT MB_Posts.* FROM MB_Posts INNER JOIN ( SELECT ID FROM MB_Posts ORDER BY Created_On DESC LIMIT 0, 100 ) as MostRecentPosts ON MB_Posts.ID = MostRecentPosts.ID ORDER BY Thread_ID, Created_On DESC;
Based on your revision, βSet the most recent posts, one per thread ID,β a table created from a subquery can be created using a small amount of concat and compare magic. This will depend on what your column types are, but they should work for most. The idea of ββboth of these solutions is to use a subquery to process your aggregation process and determine that the identifier has the required records, and then join the table against the results of this subquery to get the rest of the data for the row.
SELECT MB_Posts.* FROM MB_Posts INNER JOIN ( SELECT TRIM(LEADING '0' FROM SUBSTRING(MAX(CONCAT(UNIX_TIMESTAMP(Created_On), LPAD(ID,15,'0'))), -15)) AS ID FROM MB_Posts GROUP BY Thread_ID ) as MostRecentPostPerThread ON MB_Posts.ID = MostRecentPostPerThread.ID ORDER BY Thread_ID, Created_On DESC;
source share