How to JOIN a COUNT from a table, and then effect that COUNT with another JOIN

Posted by jakenoble on Stack Overflow See other posts from Stack Overflow or by jakenoble
Published on 2010-04-10T10:20:04Z Indexed on 2010/04/10 10:23 UTC
Read the original article Hit count: 580

Filed under:
|
|
|
|

Hi

I have three tables

Post

ID  Name
1   'Something'
2   'Something else'
3   'One more'

Comment

ID  PostId  ProfileID  Comment
1   1       1          'Hi my name is' 
2   2       2          'I like cakes'
3   3       3          'I hate cakes'

Profile

ID  Approved
1   1          
2   0          
3   1          

I want to count the comments for a post where the profile for the comment is approved

I can select the data from Post and then join a count from Comment fine. But this count should be dependent on if the Profile is approved or not.

The results I am expecting is

CommentCount

PostId  Count
1       1
2       0
3       1

Thanks for any help.

© Stack Overflow or respective owner

Related posts about sql

Related posts about join