Thanks Mike - suggested what was needed. The final query is of course a bit more complicated, but the basis was laid with a bang.
SELECT t1.parent_id, t1.id id, t1.name, t2.* FROM forum_dirs t1 LEFT JOIN ( SELECT forum_topics.title, forum_topics.dir_id, forum_topics.id topic_id, forum_topics.date topic_date, forum_topics.user_id topic_user_id, users.logo topic_user_logo, users.name topic_user_name, users.login topic_user_login, t3.* FROM forum_topics LEFT JOIN users ON users.id=forum_topics.user_id LEFT JOIN ( SELECT forum_msgs.text, forum_msgs.user_id msg_user_id, forum_msgs.date msg_date, forum_msgs.topic_id msg_topic_id, users.logo msg_user_logo, users.name msg_user_name, users.login msg_user_login FROM forum_msgs LEFT JOIN users ON users.id=forum_msgs.user_id WHERE (topic_id, date) IN (SELECT topic_id, max(date) FROM forum_msgs GROUP BY topic_id) ) t3 ON t3.msg_topic_id=forum_topics.id WHERE (dir_id, date) IN (SELECT dir_id, max(date) FROM forum_topics GROUP BY dir_id) ) t2 ON t2.dir_id=t1.id
GROUP BYdoes not know how to work with pre-sorted data, it sorts them by itself as it sees fit. You are probably writing toselect *and makinggroup byexpect that it returns for each group some specific value of some specific field. BUT what value it will give is undefined, do not think that it will give for example the first value. he has the right to give arbitrary. In addition, in many versions of MySQL, it is already forbidden to use columns with no aggregate functions in group by by default in queries. - Mikeselect * from table where (topic_id, date) IN(select topic_id, max(date) from table group by topic_id)- Mike