Counting within a query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
olegu
Forum Newbie
Posts: 5
Joined: Sat Jan 11, 2003 7:59 pm
Location: Norway

Counting within a query

Post 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
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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 :) )
olegu
Forum Newbie
Posts: 5
Joined: Sat Jan 11, 2003 7:59 pm
Location: Norway

I got it to work!!

Post by olegu »

I got it to work that way, and it wasn't slow at all!
Thank you very much for the help

-Vidar
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

my pleasure.
Post Reply