On 1 Aug, 06:27, Neeper <nee....DeleteThis@hotmail.com> wrote:
> I'm having trouble coming up with a query to count the number oftotalitemseach subcategory with the lowest cost (CPU item).
>
> I'd like to be able to display thetotalin my pages like sousing
> PHP.
>
> Automotive (4items)
> - Cars (2items)
> - Trucks (2items)
> - Vans (0items)
> Electronics (6items)
> - Digital Cameras (0items)
> - Portable Audio (3items)
> - Home Theatre & Projectors (0items)
> - TV, DVD & VCR Players (3items)
> - Sound Systems & Components (0items)
>
> At the moment I amusing4 seperate queries in loops and I know there
> must be an more efficient and easier way.
>
> For example, I'd like to count the number ofitemsthere are under the
> Automotive category such as:
>
> Automotive has 4itemswithin it (under Cars, Trucks & Vans)
> 1) 1995 Nissan Pickup
> 2) 2002 Acura RSX
> 3) 2005 Honda Civic
> 4) 2007 Dodge Durango
>
> Also is it better, faster to run the query and store the results to a
> seperate table (ie. hourlyusingcron) and retrieve thetotalfrom
> that table when needed or is it quick enough to execute that query x
> times depending on the number of categories on that page.
>
> These are the 2 tables I have:
>
> Categories table (Usinghierarchicaldatastructure)
> +--------------+-----+------+------------------------------+
> | category_id | lft | rgt | name |
> +--------------+-----+------+------------------------------+
> | 1 | 1 | 22 | Categories |
> | 2 | 2 | 9 | Automotive |
> | 3 | 3 | 8 | Cars |
> | 4 | 4 | 7 | Trucks |
> | 5 | 5 | 6 | Vans |
> | 5 | 10 | 21 | Electronics |
> | 6 | 11 | 20 | Digital Cameras |
> | 7 | 12 | 19 | Portable Audio |
> | 8 | 13 | 18 | Home Theatre & Projectors |
> | 9 | 14 | 17 | TV, DVD & VCR Players |
> | 10 | 15 | 16 | Sound Systems & Components |
> +--------------+------+-----+------------------------------+
>
> Itemstable
> +----------+-------------+--------------------------+
> | item_id | category_id | title |
> +----------+-------------+--------------------------+
> | 1 | 4 | 1995 Nissan Pickup |
> | 2 | 3 | 2002 Acura RSX |
> | 3 | 3 | 2005 Honda Civic |
> | 4 | 4 | 2007 Dodge Durango |
> | 5 | 7 | iPod Nano |
> | 6 | 7 | Creative Zen |
> | 7 | 9 | RCA 21" TV |
> | 8 | 9 | Sony 36" TV |
> | 9 | 9 | Sony 60" Projection TV |
> | 10 | 9 | Toshiba 27" TV |
> | 11 | 7 | iPod Mini |
> +----------+-------------+--------------------------+
This was answered yesterday in comp.lang.php