English 中文(简体)
论坛 MySQL 类, 线索, 邮
原标题:Forum MySQL Category, Thread, Post

我知道有一堆类似的线条,我仔细看了一看许多线索,但找不到解决办法。因此,我有一个名为“论坛”的表格,它包含我的类别(num_type=0)、线索(num_type=1)和邮递(num_type=2)。有一个专栏 num_parent,它将把线索/消息与父/母分类/线索联系起来。关于我遇到的问题,我正试图将所有类别、每个类别内的总线索以及每个类别线索内的总职位都包括在内。要做到这一点,我正在尝试:

select F.*
, count(F2.num_Forum) as num_ThreadCount
, count(F3.num_Forum) as num_PostCount
, max(F3.dte_Created) as dte_LastPost
from forum F
left outer join forum F2
on F.num_Forum=F2.num_Parent and F2.num_Type=1
left outer join forum F3
on F2.num_Forum=F3.num_Parent and F3.num_Type=2
where F.num_Type=0 and F.num_Inactive=0
group by F.num_Forum
order by F.num_Sticky desc, F.dte_Created desc

dte_ LastPost 和 num_ PostCount 正在正确显示。 num_ TreadCount 并不正确。 如果我将查询分解为两个分隔查询 :

select F.* 
, count(F2.num_Forum) as num_ThreadCount
from forum F
left outer join forum F2
on F.num_Forum=F2.num_Parent and F2.num_Type=1
where F.num_Type=0 and F.num_Inactive=0
group by F.num_Forum
order by F.num_Sticky desc, F.dte_Created desc

select count(F3.num_Forum) as num_PostCount
from forum F2
left outer join forum F3
on F2.num_Forum=F3.num_Parent and F3.num_Type=2
where F2.num_Type=1 and F2.num_Inactive=0
group by F2.num_Forum
order by F2.num_Sticky desc, F2.dte_Created desc

我对每个问题都得到了正确的计算。 但是, 我需要将这些计算组合起来, 这样我才能知道 Num_ PostCount 与哪个类别相对应。 我分别做这个区分时看到的最大区别是, 在第二个查询中, 我能够用 F2. num_ Forum 进行分组。 我试图将它添加到联合查询中, 但是它没有解决我的问题。 有人知道我需要做些什么来修正我的第一个查询吗?

最佳回答

Ok,我找到了连接三个表格的查询方法,没有在职位表中设置一栏,直接连接到该类别。相反,类别链接到与职位、上下链条相连的线索。

select C.*
, count(TP.num_Thread) as num_ThreadCount
, coalesce(sum(TP.num_PostCount), 0) as num_PostCount
from forum_categories C 
left join 
(select count(P.num_Post) as num_PostCount, 
T.num_Thread, T.num_CParent, T.num_Inactive
from forum_threads T
left outer join forum_posts P
on P.num_TParent=T.num_Thread and P.num_Inactive=0
where T.num_Inactive=0
group by T.num_Thread) TP 
on C.num_Category=TP.num_CParent and TP.num_Inactive=0
where C.num_Inactive=0
group by C.num_Category
order by C.num_Sticky desc, C.dte_Created desc
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签