get count of childs and childs of childs
an anonymous user
·
SQL
-- create a table CREATE TABLE forum ( forum_id INTEGER PRIMARY KEY, name TEXT NOT NULL ); -- create a table CREATE TABLE topic ( topic_id INTEGER PRIMARY KEY, forum_id INTEGER NOT NULL, name TEXT NOT NULL ); -- create a table CREATE TABLE post ( post_id INTEGER PRIMARY KEY, topic_id INTEGER NOT NULL, name TEXT NOT NULL ); INSERT INTO forum VALUES (1, 'Animals'); INSERT INTO forum VALUES (2, 'Technology'); INSERT INTO topic VALUES(1, 1, 'Isnt lion nice?'); INSERT INTO topic VALUES(2, 1, 'Zebras are awesome!'); INSERT INTO topic VALUES(3, 2, 'Dell xps 15 questions'); INSERT INTO post VALUES(1, 1, 'post 1'); INSERT INTO post VALUES(2, 1, 'post 2'); INSERT INTO post VALUES(3, 1, 'post 3'); INSERT INTO post VALUES(4, 2, 'post 4'); INSERT INTO post VALUES(5, 2, 'post 5'); INSERT INTO post VALUES(6, 2, 'post 6'); INSERT INTO post VALUES(7, 2, 'post 7'); INSERT INTO post VALUES(8, 3, 'post 8'); select f.name, count(distinct t.topic_id) as topics_count, count(p.post_id) as posts_count from forum f inner join topic t on t.forum_id = f.forum_id inner join post p on p.topic_id = t.topic_id group by f.forum_id;
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.