我正在工作'现在这个功能两个小时了。我做错了什么,我无法得到什么。
对于这个例子,我有两个表:
Table 1 : fo_forums
ID | parent (id) | title
Table 2 : fo_topics
ID | parent (id) | title
一个论坛可以有父母(也是一个论坛),这个父母可以有一个父母(另一个论坛),但最后一个不能拥有父母。 (所以,我们可以有3代论坛)
主题是父母,这是一个论坛。
我正在尝试执行一个简单的事情:获取一个论坛列表,其中包含他们内部的主题数量以及他们潜在的孩子(0到2个孩子之间)。
我测试了很多东西,我做的最后一次是:
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
通过这个查询,我得到了一些不连贯的数字......我真的不知道如何写它。
Plz通知:
感谢您的帮助。
答案 0 :(得分:0)
经过几个小时的工作(太多小时,tbh),我能得到的最好的是那两个版本:(用法语,plz不要打扰)
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
并且:
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
有了这个,我可以得到:
这有点凌乱,至少在我看来,我很确定有更好的方法和更好的方法来做到这一点,但我无法弄清楚如何。
非常令人惊讶,至少对我来说,第二个版本比第一个版本更快。
如果有人想改进这个,我会接受建议。与此同时,这将是我的功能!