我正在用PHP编写一个论坛,所以我来到了我需要计算论坛中线程数量的部分。我正在使用以下查询来获取所有论坛及其各自的类别:
SELECT f.id
, f.name
, f.description
, c.id category_id
, c.name category_name
, c.description category_description
FROM forum_forums f
JOIN forum_forums_categories fc
ON f.id = fc.forum_id
JOIN forum_categories c
ON fc.category_id = c.id;
它完成了工作,然后我可以将所有内容分组。我接下来要做的是,还将某个论坛中的线程数量添加到结果中的每一行,我不确定该怎么做。
我有以下表格:forum_forums
,forum_threads
,forum_categories
。此外,线程可以属于多个论坛(我有一个forums_threads_forums
表,它将特定的thread_id
绑定到forum_id
。
所以我的猜测是我需要在原来的命令中添加一个计数。
此计数需要计算forum_threads_forums
表中的行,其中forum_id
等于它添加到结果中的当前论坛的行。
为了简单起见,这里是我想要实现(简化)的一个例子:
表:forum_forums
id name
1 forum1
2 forum2
3 forum3
表:forum_threads
id title
1 thread1
2 thread2
表:forum_threads_forums
thread_id forum_id
1 1
1 2
2 1
2 3
然后我希望查询返回(除其他外):
forum_forums.id forum_forums.name forum.threads
1 forum1 2
2 forum2 1
3 forum3 1
如果有人能够把我推向正确的方向,那就太好了。
修改
我想我可能需要一个子查询,例如SELECT COUNT(thread_id) AS thread_count FROM forum_threads_forums WHERE forum_id=:forum_id
,但我不确定在原始查询中将其放在哪里
解答:
以备将来参考,这是我现在正在使用的工作命令:
SELECT
forum_forums.id,
forum_forums.name,
forum_forums.description,
COUNT(forum_threads_forums.thread_id) AS thread_count,
forum_categories.id AS category_id,
forum_categories.name AS category_name,
forum_categories.description AS category_description
FROM
forum_forums
LEFT OUTER JOIN
forum_threads_forums
ON
forum_forums.id=forum_threads_forums.forum_id
INNER JOIN
forum_forums_categories
ON
forum_forums.id=forum_forums_categories.forum_id
INNER JOIN
forum_categories
ON
forum_forums_categories.category_id=forum_categories.id
GROUP BY
forum_forums.id
答案 0 :(得分:1)
您可以使用LEFT JOIN和GROUP BY:
SELECT f.id
, f.name
, f.description
, c.id category_id
, c.name category_name
, c.description category_description,
, COUNT(ft.thread_id) as threads
FROM forum_forums f
LEFT JOIN forum_forums_categories fc
ON f.id = fc.forum_id
LEFT JOIN forum_categories c
ON fc.category_id = c.id
LEFT JOIN forum_threads_forums ft
ON f.id = ft.forum_id
GROUP BY f.id
答案 1 :(得分:1)
我怀疑是否有关于聚合函数的入门级教程未能涵盖这一点,但无论如何......
DROP TABLE IF EXISTS forums;
CREATE TABLE forums
(forum_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,forum_name VARCHAR(12) NOT NULL UNIQUE
);
INSERT INTO forums VALUES
(1 ,'forum1'),(2,'forum2'),(3,'forum3');
DROP TABLE IF EXISTS threads;
CREATE TABLE threads
(thread_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,title VARCHAR(12) NOT NULL
);
INSERT INTO threads VALUES
(1 ,'thread1'),
(2 ,'thread2');
DROP TABLE IF EXISTS threads_forums;
CREATE TABLE threads_forums
(thread_id INT NOT NULL
,forum_id INT NOT NULL
,PRIMARY KEY(thread_id,forum_id)
);
INSERT INTO threads_forums VALUES
(1 ,1),
(1 ,2),
(2 ,1),
(2 ,3);
SELECT f.*
, COUNT(t.thread_id) threads
FROM forums f
JOIN threads_forums tf
ON tf.forum_id = f.forum_id
JOIN threads t
ON t.thread_id = tf.thread_id
GROUP
BY forum_id;
+----------+------------+---------+
| forum_id | forum_name | threads |
+----------+------------+---------+
| 1 | forum1 | 2 |
| 2 | forum2 | 1 |
| 3 | forum3 | 1 |
+----------+------------+---------+
请注意,此解决方案不会显示没有线程的论坛。为此,您需要使用LEFT [OUTER] JOIN而不是
答案 2 :(得分:0)
要获得简化示例的预期结果,请运行
SELECT f.id, f.name, COUNT(*) AS threads
FROM `forum_forums` f
INNER JOIN `forum_threads_forums` r ON r.forum_id = f.id
INNER JOIN `forum_threads` t ON t.id = r.thread_id
GROUP BY f.id
ORDER BY f.id
答案 3 :(得分:0)
在伪SQL中:
SELECT f.id, f.name, COUNT(t.thread_id) FROM forum_threads_forums AS t
JOIN forum_forums AS f ON t.forum_id=f.id
GROUP BY t.forum_id
答案 4 :(得分:0)
使用更简单的表:
SELECT
forum_forums.id,
forum_forums.name,
COUNT(forum_threads_forums.thread_id) as threadCount
FROM
forum_forums JOIN forum_threads_forums on forum_threads_forums.forum_id = forum_forums.id
GROUP BY
forum_forums.id