Есть 3 таблицы - forum_dirs - структура форума(разделы - AL(через parant_id)), forum_topics - темы, forum_msgs - сообщения к темам.
Хочу одним запросом выдернуть все разделы форума, одновременно взяв последнюю тему и если есть сообщение в этой теме. Одновременно посчитав количество сообщений и тем в каждом разделе(это пока опущу в примере).
Проблема в том, что Mysql выполняет сначала группировку, потом сортировку, т.е. я не могу сделать запрос типа.
forum_topics
SELECT
forum_dirs.*,forum_topics.id topic_id,forum_topics.dir_id dir_id, forum_topics.title,forum_topics.date topic_date,forum_msgs.id msg_id,forum_msgs.date msg_date,forum_msgs.text msg_text,users.logo,users.name user_name,users.login user_login
FROM forum_dirs
LEFT JOIN forum_topics ON forum_topics.dir_id=forum_dirs.id
LEFT JOIN forum_msgs ON forum_msgs.topic_id=forum_topics.id
LEFT JOIN users ON forum_msgs.user_id=users.id
ORDER BY
forum_dirs.id ASC,
forum_dirs.parent_id ASC,
forum_dirs.pos ASC,
forum_topics.date DESC,
forum_msgs.date DESC
GROUP BY dir_id
Порылся на форумах, все советуют сделать запросы типа.
SELECT * FROM (
SELECT
forum_dirs.*,forum_topics.id topic_id,forum_topics.dir_id dir_id, forum_topics.title,forum_topics.date topic_date,forum_msgs.id msg_id,forum_msgs.date msg_date,forum_msgs.text msg_text,users.logo,users.name user_name,users.login user_login
FROM forum_dirs
LEFT JOIN forum_topics ON forum_topics.dir_id=forum_dirs.id
LEFT JOIN forum_msgs ON forum_msgs.topic_id=forum_topics.id
LEFT JOIN users ON forum_msgs.user_id=users.id
ORDER BY
forum_dirs.id ASC,
forum_dirs.parent_id ASC,
forum_dirs.pos ASC,
forum_topics.date DESC,
forum_msgs.date DESC
) p1 GROUP BY dir_id
Но тут получается та же самая ерунда не отсортированная на выходе.
Помогите как можно составить запрос?