Count Comments?

Reply

Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Count Comments?

 
0
  #1
Jul 30th, 2008
Hi, Thanks in advance for any help someone could offer.

I'm trying to display a list of stories submitted to my site from a table titled stories, the name of the person who submitted the story and how many comments about the story have been posted.

I'm working with three tables.

The Stories table [story_id] [user_id] [storytitle] [storycategory]
The Team Table [team_id] [teamname]
The Comments Table [comment_id] [story_id] [user_id]

my query should be something like:
Get the stories
(select * from stories)
Also the team name of the person who submitted them
	(inner join teams ON stories.user_id = teams.team_id) 
And count for me how many times a comment has been made for each story.
	(count how many comments.story_id =stories.story_id)

i can easily obtain the story titles and the team names that submitted with
  1. SELECT * FROM stories
  2. INNER JOIN teams ON stories.user_id = teams.team_id
but I have no idea how to count how many comments.story_id = stories.story_id

again,
thanks in advace.
Last edited by dottomm; Jul 30th, 2008 at 4:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Count Comments?

 
0
  #2
Jul 31st, 2008
  1. $r=mysql_query("select count(*) as cnt form stories inner join teams ON stories.user_id = teams.team_id");
  2. $t=mysql_fetch_array($r);
  3. echo $t['cnt'];//prints count of stories..
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Count Comments?

 
0
  #3
Jul 31st, 2008
Thank you Shanti for your help, unfortunately that isn't quite what I am trying to do. Instead of counting how many stories are posted, or how many comments are posted, I want to know how many comments are tied to each individual story.

  1. How many comments.story_id = stories.story_id
  2.  
  3. so my result would be:
  4. story_id | story title | # of comments (from comment table)

So the query needs to be something like:
echo all the story titles and story_ids and the number of comments each record has received.

Again, thanks for any help or clues.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Count Comments?

 
0
  #4
Aug 1st, 2008
try this:
  1. $r=mysql_query("select count(*) as cnt form commenttable comments.story_id = stories.story_id
  2. ");
  3. $t=mysql_fetch_array($r);
  4. echo $t['cnt'];//prints count of stories..

post your table columns names clearly....
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Count Comments?

 
0
  #5
Aug 4th, 2008
Again, thank you, but no luck.

I'm not sure what is wrong.

As an example, it would be like how the Topic/Threads listed on this site.

Thread Name | Replies | Views

I'm sure that info all lives in separate tables.
Can anybody take a stab at guess what the select statement would look like be on this page:
http://www.daniweb.com/forums/forum17.html


Thanks in advance,

-dottomm
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 16
Reputation: mcd is an unknown quantity at this point 
Solved Threads: 6
mcd mcd is offline Offline
Newbie Poster

Re: Count Comments?

 
0
  #6
Aug 5th, 2008
try this (name the fields you want to select instead of using the star give you more versatility - like a sub-query):

  1. SELECT s.story_id, s.user_id, s.storytitle, t.team_name
  2. (SELECT COUNT(c.story_id) FROM comments c
  3. WHERE c.story_id = s.story_id) AS comment_cnt
  4. FROM stories s
  5. INNER JOIN teams t ON s.user_id = t.team_id

comment_cnt should then be the number you're looking for. Note that this will not pull any of the comments themselves, just the count in each story.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Count Comments?

 
0
  #7
Aug 6th, 2008
Yes! That is exactly what I am trying to do. Thank you for understanding.

However, I am stumped by how to display the 'comment_cnt'. I've not used the 'AS' statement before. How do I pass that as a variable?


thanks for getting me this far.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Count Comments?

 
0
  #8
Nov 7th, 2008
Sorry to dig this old thread up again, but I am once again faced with a similar query problem.

I am try to to count how many posts a user has made to a forum

i have two tables:

t1 Posts
--------
id
username
------------

t2 users
------
user_id
username
title

Here is the query I an using and I can't figure out why It's not working. I keep getting an error on the line where the sub-query starts.

(Select users.user_id,users.username,posts.id  
  (select COUNT(posts.id) AS post_count from posts where users.user_id=posts.user_id)

in English; I want to display the users "username" and how many times the users "user_id" appears in the user_id column on the Posts table as 'post_count'.

Thanks in advance for any help anyone can offer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Count Comments?

 
0
  #9
Nov 7th, 2008
  1. SELECT
  2. count(*),
  3. p.post_id,
  4. p.user_id,
  5. u.username
  6. FROM
  7. Posts p
  8. INNER JOIN
  9. Users u
  10. on
  11. p.user_id = u.user_id
  12. WHERE
  13. p.user_id = ?

that will be for a single user, if you want something for a view, remove the where and query the view
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 53
Reputation: dottomm is an unknown quantity at this point 
Solved Threads: 0
dottomm dottomm is offline Offline
Junior Poster in Training

Re: Count Comments?

 
0
  #10
Nov 25th, 2008
Yes! That is it. Thank you. I'm learning a lot here. Thank you everyone.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC