Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
Jamroom Developers:
need to get a count of ads in classified ads category
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 11/14/09 14:03 
I am having an issue writing the SQL statement to retrieve all of the category information from the category table as well as a count of ads within each category. I want to be able to add this feature to the ranking.php script for my classified ads module. This way I can have the category list look something like:

For Sale (225 Ads)
Musicians Looking (5 Ads)
etc.

    Category Table:
    category_id INT(11)
    category_title VARCHAR(50)
    category_description VARCHAR(255)

    Ads Table:
    ad_id INT(11)
    ad_category_id INT(11) * This is the column that joins the 2 tables together.

I figured I would be able to do it with something like:

Code

SELECT c.*, COUNT(a.ad_id) AS adCount
FROM {$jamroom_db['mcCategories'] c
LEFT JOIN {$jamroom_db['mcAds']} a ON a.ad_category_id=c.category_id


But all that does is return the total count of ads within the ad table.

Any suggestions would be greatly appreciated,
-Tim

Back to top
SteveX
Ultrabubble


Joined: 30 Aug 2005
Posts: 8792
Location: Ultrabubble

Posted: 11/14/09 14:22 
I'm no expert but try a GROUP BY:

Code
SELECT c.*, COUNT(a.ad_id) AS adCount
FROM {$jamroom_db['mcCategories'] c
LEFT JOIN {$jamroom_db['mcAds']} a ON a.ad_category_id=c.category_id
GROUP BY c.category_title ASC



_________________
Kulshi Mezian!

"Stranger from another planet, welcome to our hole. Just strap on your guitar and we'll play some rock and roll"

Ultrabubble create things.
Back to top
musiccreatures



Joined: 20 Jun 2009
Posts: 344
Location: Portland Oregon

Posted: 11/14/09 14:31 
Brilliant! It works like a champ. Thank you so much SteveX. Your a gentleman and a scholar.

-Tim

Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
Jamroom Developers

 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.