2017-03-08 3 views
0

Ich arbeite jetzt seit zwei Stunden an dieser Funktion. Es gibt etwas, was ich falsch mache und ich kann nicht genau was bekommen.Meine Themen-mysql-query-counter funktioniert nicht

ich habe, für dieses Beispiel zwei Tabellen:

Table 1 : fo_forums 
ID | parent (id) | title 

Table 2 : fo_topics 
ID | parent (id) | title 

Ein Forum kann einen Elternteil hat (das ist ein Forum zu ist), und dieser Elternteil kann einen Elternteil hat (ein anderes Forum), aber diese Der Letzte kann keinen Elternteil haben. (So können wir nur 3 Generationen von Foren haben)

Ein Thema hat ein Elternteil, das ein Forum ist.

Ich versuche, eine einfache Sache durchzuführen: erhalten Sie eine Liste der Foren mit der Anzahl der Themen in ihnen UND in ihren potenziellen Kindern (zwischen 0 und 2 Kinder).

ich viele Dinge, die letzte habe ich getestet ist:

SELECT fo.fo_id, 
(SELECT COUNT(fot_id) FROM fo_topics 
    WHERE fot.parent = fo.fo_id OR fot.parent = foc1.fo_id OR fot.parent = foc2.fo_id 
) as count 
FROM fo_forums as fo 
LEFT JOIN fo_forums as foc1 ON foc1.fo_parent = fo.fo_id 
LEFT JOIN fo_forums as foc2 ON foc2.fo_parent = foc1.fo_id 

Mit dieser Abfrage, bekomme ich einige inkohärente Zahlen ... Ich weiß wirklich nicht, wie es zu schreiben.

Plz Hinweis:

  • Ich verwende MySQL
  • dies ist ein simplify query (ich andere Sachen über das Forum in der „realen Abfrage“, aber mit oder ohne diese Dinge ändert es nichts)

Danke für Ihre Hilfe.

Antwort

0

Nach einigen Stunden Arbeit (zu viele Stunden, TBH), das Beste, was ich bekommen konnte, ist diese 2 Versionen: (in französisch ist, PLZ nicht stören)

Version 1: 
SELECT DISTINCT fo.fo_id, fo.fo_parent, fo.fo_type, fo.fo_statut, fo.fo_ordre, fo.fo_titre, 
    COUNT(DISTINCT fot.fot_id) as nbr_topics, 
    COUNT(DISTINCT fot2.fot_id) as nbr_topics_enfants, 
    COUNT(DISTINCT fop.fop_id) as nbr_posts, 
    COUNT(DISTINCT fop2.fop_id) as nbr_posts_enfants, 
    fopi.fop_id, fopi.fop_timestamp, fopi.fop_fotid, fopi.fop_uid, u.u_pseudonyme, u.u_avatar, foti.fot_id, foti.fot_titre 
FROM fo_forums as fo 
    LEFT JOIN fo_forums as foe ON foe.fo_parent = fo.fo_id 
    LEFT JOIN fo_forums as fope ON fope.fo_parent = foe.fo_id 
    LEFT JOIN fo_topics as fot ON (fot.fot_foid = fo.fo_id) 
     LEFT JOIN fo_posts as fop ON fop.fop_fotid = fot.fot_id 
    LEFT JOIN fo_topics as fot2 ON (fot2.fot_foid = foe.fo_id OR fot2.fot_foid = fope.fo_id) 
     LEFT JOIN fo_posts as fop2 ON fop2.fop_fotid = fot2.fot_id 
    LEFT JOIN fo_posts as fopi ON fopi.fop_id = (SELECT MAX(fot_derniermsg) FROM fo_topics WHERE (fot_foid = fo.fo_id)) 
     LEFT JOIN fo_topics as foti ON foti.fot_id = fopi.fop_fotid 
     LEFT JOIN u_individus as u ON u.u_id = fopi.fop_uid 
GROUP BY fo.fo_id 

Und:

Version 2 
SELECT DISTINCT fo.fo_id, fo.fo_parent, fo.fo_type, fo.fo_statut, fo.fo_ordre, fo.fo_titre, 
    (SELECT COUNT(fot.fot_id) FROM fo_topics as fot WHERE fot.fot_foid = fo.fo_id) as nbr_topics, 
    (SELECT COUNT(fot.fot_id) FROM fo_topics as fot 
     LEFT JOIN fo_forums as foe ON foe.fo_id IS NOT NULL 
     LEFT JOIN fo_forums as fope ON fope.fo_parent = foe.fo_id 
    WHERE ((foe.fo_parent = fo.fo_id) AND (fot.fot_foid = foe.fo_id OR fot.fot_foid = fope.fo_id)) 
    ) as nbr_topics_enfants, 
    (SELECT COUNT(fop.fop_id) FROM fo_posts as fop JOIN fo_topics as fot ON fot.fot_id = fop.fop_fotid WHERE fot.fot_foid = fo.fo_id) as nbr_posts, 
    (SELECT COUNT(fop.fop_id) FROM fo_topics as fot 
     LEFT JOIN fo_forums as foe ON foe.fo_id IS NOT NULL 
     LEFT JOIN fo_forums as fope ON fope.fo_parent = foe.fo_id 
     LEFT JOIN fo_posts as fop ON fop.fop_fotid = fot.fot_id 
    WHERE ((foe.fo_parent = fo.fo_id) AND (fot.fot_foid = foe.fo_id OR fot.fot_foid = fope.fo_id)) 
    ) as nbr_posts_enfants, 
    fopi.fop_id, fopi.fop_timestamp, fopi.fop_fotid, fopi.fop_uid, u.u_pseudonyme, u.u_avatar, foti.fot_id, foti.fot_titre 
FROM fo_forums as fo 
    LEFT JOIN fo_posts as fopi ON fopi.fop_id = (SELECT MAX(fot_derniermsg) FROM fo_topics WHERE (fot_foid = fo.fo_id)) 
     LEFT JOIN fo_topics as foti ON foti.fot_id = fopi.fop_fotid 
     LEFT JOIN u_individus as u ON u.u_id = fopi.fop_uid 

Damit kann ich:

  • alle grundlegenden Informationen des Forums (Titel, Status ...)
  • die Anzahl der Themen direkt mit dem Forum verbunden
  • die Anzahl der Themen des Forums Kinder verbunden (das heißt: andere Foren)
  • die Zahl der Stellen in den Themen des Forums
  • die Anzahl der Beiträge in die Themen des Forums Kinder
  • die Informationen (id, Datum und Infos des Benutzers) des letzten Beitrag/Themen im Forum

Es ist ein wenig chaotisch, zumindest meiner Meinung nach, und ich bin ziemlich Sicher, es gibt einen schöneren Weg und eine bessere Leistung, aber ich kann nicht finde heraus, wie.

Ziemlich erstaunlich, für mich zumindest, ist die zweite Version schneller als die erste.

Wenn jemand eine Idee hat, dies zu verbessern, bin ich offen für Vorschläge. In der Zwischenzeit wird es meine Aufgabe sein!