Page 1 of 1

Counting within a query

Posted: Sat Jan 11, 2003 7:59 pm
by olegu
OK first: I'm quite new to MySql and PHP so this is perhaps a stupid question, but I'll give it a shot.

On my site (http://www.soemme.no/index2.php?numero=1)I have a section with news and stuff, with the posibility for users to add comments to each "news story". What I'm trying to do is counting how many comments there are for each story and presenting it in the link to the comments section like this : [comments (6)].
I've found that this should be possible to do with adding a query for each news story (repeat pattern thing?), and counting the records for each query. This will however lead to a high strain on the server, and a slow site, wich of course is no good.
What I want to do is to have one query wich collects the "title" field from the entire comments table (for all the news storys) and then have a litle bit of code for each story that counts how many comments there are for that title.

Can this be done, if it can I would really appreciate any help.

And BTW, I'm new in here: hello every one :D

-Vidar

Posted: Sat Jan 11, 2003 10:53 pm
by evilcoder
welcome.

first off, by doing it the first way (the way you said will have performance problems) is probably the most simple way of getting it done, and contrary to what you may think the performance decrease will not be noticable to a mere human being.

For the time being go with your first thought, i will work out a better way and post it soon. i have to go and slap this idiot for money he owes me. (other side of the office :) )

I got it to work!!

Posted: Sun Jan 12, 2003 6:20 am
by olegu
I got it to work that way, and it wasn't slow at all!
Thank you very much for the help

-Vidar

Posted: Sun Jan 12, 2003 7:26 am
by evilcoder
my pleasure.