Yxvasznalskje
Yxvasznalskje

Reputation: 62

Join two tables and get number of rows

I need to get the number of posts not topics, where forum_id = 1. Is this even possible? I guess I have to use join but this seems incredible hard for me.

topics:
id | title | forum_id

posts:
id | message | topic_id

Upvotes: 2

Views: 54

Answers (2)

Sanath
Sanath

Reputation: 4886

number of posts with a topic

 select distinct count(p.id) from posts p inner join topics t on p.topic_id = t.id 
        where t.forum_id = 1
        group by p.id

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

SELECT COUNT(*) FROM posts JOIN topics t ON (topic_id = t.id) WHERE forum_id = 1

Upvotes: 3

Related Questions