Gossamer Forum
Home : General : Perl Programming :

Ack...SQL query....

Quote Reply
Ack...SQL query....
I'm writing a script at the moment that can have unlimited levels of categories

In the database I store the ID, ParentID, Title, Full Title etc...so an example top-level cat and subcat may look like this in the database:

1 1 Category_One Category_One
2 1 SubCategory Category_One/SubCategory

Now the problem Im having is that Im adding "galleries" to a seperate table and next to category names in the templates I want to show the number of galleries in each category. At the moment it is fine as I have a Total Column in the categories table and as you can see from the example above I can just increment the Total where the parentid equals one. However with three levels of categories it looks like:

1 1 Category_One Category_One
2 1 SubCategory Category_One/SubCategory
3 2 SubSubCat Category_One/SubCategory/SubSubCat

....I just totally can't figure out how to increment all totals for the categories in the tree above and including the category the gallery is being added to.

:(

Last edited by:

RedRum: Dec 23, 2001, 1:34 PM
Quote Reply
Re: [RedRum] Ack...SQL query.... In reply to
Would I be better forgetting the Total column and using COUNT() instead?
Quote Reply
Re: [RedRum] Ack...SQL query.... In reply to
Im using this now and it seems to work fine (although a little slower):

$DB->select( ['COUNT(*)'], 'Table', { G_CatID => $cat>{C_ID} )->fetchrow;

It is in a loop so outputs something like:

SELECT COUNT(*) FROM Table WHERE G_CatID=1
Quote Reply
Re: [RedRum] Ack...SQL query.... In reply to
Why COUNT(*)? Wouldn't it be faster just to COUNT(ID) a paticular field?

- wil
Quote Reply
Re: [Wil] Ack...SQL query.... In reply to
COUNT(*) is what I need.

http://www.mysql.com/...o/Counting_rows.html