i have table called story_comment (integer) columns story_id, , comment_id. want know how many comments each story has i'm not sure best way write sql query.
the query should return table columns story_id , num_comments (where num_comments number of rows in story_comment story_id same story_id in results row).
desired results (example):
story_id | num_comments
4 | 17 6 | 0 7 | 4
i able 1 particular story_id query:
select story_id, count(story_id) num_comments story_comment story_id=20;
but i'm not sure how can every story_id in table. side note i'm going doing query using mysqli in php.
use group by
select story_id, count(story_id) num_comments story_comment group story_id
the group statement used in conjunction aggregate functions group result-set 1 or more columns.
Comments
Post a Comment