Snafu: Meine Lösung

Beitrag lesen

So,
meine endgültige Lösung ist nun doch etwas anders als die Zwischenlösungen. Diese Abfrage tut nun endlich das, was ich haben will und das bedeute ich bekomme:

*) forum_forums: id, admin, title, description
*) Subabfragen: Anzahl der Topics und  Posts in einem Forum
*) forum_posts: name, datetime
*) forum_topics: topic

zurückgeliefert.

--- MySQL Start ---  
SELECT f.id, f.admin, f.title, f.description,  
COALESCE( (SELECT COUNT( * )  
  FROM forum_topics  
  WHERE forum_id = f.id  
  GROUP BY forum_id ) , 0) AS total_topics,  
COALESCE( (SELECT COUNT( * )  
  FROM forum_posts  
  WHERE forum_id = f.id  
  GROUP BY forum_id ) , 0) AS total_posts,  
t.topic AS lastpost_topic,  
p.name AS lastpost_name, p.`datetime` AS lastpost_datetime  
FROM forum_forums AS f  
  
LEFT JOIN (  
  SELECT forum_id, topic_id, name, `datetime`  
  FROM forum_posts AS x  
  WHERE id = (  
    SELECT MAX( id )  
    FROM forum_posts  
    WHERE forum_id = x.forum_id )  
) AS p ON p.forum_id = f.id  
  
LEFT JOIN (  
  SELECT id, topic  
  FROM forum_topics  
) AS t ON t.id = p.topic_id  
ORDER BY f.admin DESC , f.id ASC  
--- MySQL End ---  

Sagt mir bitte, ob das eine vernünftie Abfrage ist oder ob man das leichter lösen könnte.

--
Lg,
Snafu