Jump to content

Count Items in each Category


barkster

Recommended Posts

I'm trying to list my categories and how many items are in each category. I'm not much on sub queries. How can I combine these two items. I'd like to display the category and the corresponding number of items in each

Cats:
Select
Category.CategoryID,
Category.Category,
Category.Sorting
From
Category
Order By
Category.Sorting Desc

Items:
Select Count(Listings.CategoryID)
From
Listings
Group By
Listings.CategoryID
Link to comment
Share on other sites

This should work (UNTESTED):

[code]Select
Category.CategoryID,
Category.Category,
Category.Sorting,
(Select Count(CategoryID)
From
Listings WHERE CategoryID = Category.CategoryID) AS cnt
From
Category
Order By
Category.Sorting Desc[/code]
Link to comment
Share on other sites

Thanks for the help, I messed with it for a while and can't get it to work. i tried the following

Select
Category.CategoryID,
Category.Category,
Category.Sorting,
(Select Count(Listings.CategoryID)
From
Listings WHERE Listings.CategoryID = Category.CategoryID) AS cnt
From
Category
Order By
Category.Sorting Desc

&

Select
Category.CategoryID,
Category.Category,
Category.Sorting,
(Select Count(Listings.CategoryID) as cnt
From
Listings WHERE Listings.CategoryID = Category.CategoryID)
From
Category
Order By
Category.Sorting Desc
Link to comment
Share on other sites

Ah, 4.0.25 glad you mentioned it, just moved to a new host and was supposed to have 4.1. Now I know why I couldn't get any sub selects to work. I did though get it to work like this

Select
Category.CategoryID,
Category.Category,
Category.Sorting,
Count(Listings.CategoryID) as cnt
From
Category LEFT JOIN Listings on(Listings.CategoryID=Category.CategoryID)
Group By
Category.CategoryID
Order By
Category.Sorting Asc


Thanks for the help.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.