I'm programming a forum in PHP, and so I've come to the part where I'll need to count the amount of threads in a forum. I'm using the following query to get all forums and their respective categories:
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;
It gets the job done and I'm then able to group everything into categories. What I want to do next, is also add the amount of threads that are in a certain forum to each row in the results, and I'm unsure how to do that.
I have the following tables: forum_forums
, forum_threads
, forum_categories
. Also, threads can belong to multiple forums (I have a forums_threads_forums
table which binds a specific thread_id
to a forum_id
).
So my guess is I would need to add a count in the original command somewhere. This count would need to count the rows in the forum_threads_forums
table where forum_id
is equal to that of the current forum it is adding to the results.
To make things simpler, here is an example of what I'm trying to achieve (simplified): Table: forum_forums
id name
1 forum1
2 forum2
3 forum3
Table: forum_threads
id title
1 thread1
2 thread2
Table: forum_threads_forums
thread_id forum_id
1 1
1 2
2 1
2 3
Then I would like the query to return (amongst other things):
forum_forums.id forum_forums.name forum.threads
1 forum1 2
2 forum2 1
3 forum3 1
If anyone could push me in the right direction that would be great.
EDIT:
I think I might need a subquery such a SELECT COUNT(thread_id) AS thread_count FROM forum_threads_forums WHERE forum_id=:forum_id
but I'm unsure where to place this in my original query
ANSWER:
For future reference, here is the working command I'm using now:
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
You can use LEFT JOIN and 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
I doubt that there's an entry level tutorial on aggregate functions that fails to cover this, but anyway...
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 |
+----------+------------+---------+
Note that this solution will not show forums for which there are no threads. For that, you'd need to use a LEFT [OUTER] JOIN instead
To get your desired result for the simplified example, run
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
In pseudo 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
Using your simpler table:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.