General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 8th, 2003, 12:42 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 sec
Reputation Power: 0
Unhappy displaying a category total subcategories

Hi

I want to know how to display the total subcategories for a category like this

category1 (total x subcats)
category2 (total x subcats)
etc....

to elaborate more see how they did it here in devscripts
http://www.devscripts.com/topicview.php?tId=1

they show a list of avaialable scripts category and in () the total of the scripts correspponding to that category.

Any idea.

Thanks

Reply With Quote
  #2  
Old July 8th, 2003, 04:10 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 sec
Reputation Power: 0
Still no ideas

Reply With Quote
  #3  
Old July 8th, 2003, 07:09 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to dhouston
Assuming a category table and a subcategory table, you might try something like the following:

SELECT category.name, count(subcategory.id) where subcategory.category_id=category.id GROUP BY category.name

However your tables are built, a GROUP BY clause is probably going to be the easiest way to do this without having a bunch of nested queries. Check out mysql.com for the usage of GROUP BY, which can be a smidgin tricky.

Reply With Quote
  #4  
Old July 8th, 2003, 09:12 AM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 sec
Reputation Power: 0
Thanks dhouston , that worked.

But this select statement shows me only the category which has a correspondent subcategory , but not category which doesn`t have a subcat.

How do I display also these categories with no subcats.

At the moment lets assume the following table structures:

Category table
catid
catname

data in category table
1 windows
2 graphics
3 internet

subcat table
subcatid
subcatname
catid

data in subcat table
1 windows95 1
2 xp 1
3 photoshop 2
4 fireworks 2
5 psp 2

as u can c category 'internet' doesn`t have a subcat , but still i want it to show in the list , how can i modify your select statement where clause to do that.

at the moment it shows:
Graphics 3
Windows 2

I want it to show:
Graphics 3
Windows 2
Internet

Thanks

Reply With Quote
  #5  
Old July 8th, 2003, 10:12 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to dhouston
Ah, maybe instead of a simple WHERE, you should use a LEFT JOIN (or is it a RIGHT JOIN?), which can be used to give you the results not found in the query. Again, check mysql.com for the syntax and the specifics on usage. Basically, you can do a left join where the right half of the join is NULL in order to get the results that don't match the original restriction. Failing that, you may just need to do two queries, one to get categories that do have subcategories and one to get the rest. You could push each type of query onto an array along with the number of subcats for each and sort the array by key prior to display.

Reply With Quote
  #6  
Old July 8th, 2003, 10:33 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to avit
Cool

That sounds about right, dhouston...

Try this, kupilot:

Code:
SELECT category.name, count(subcategory.id) FROM category LEFT JOIN subcategory USING (category.id, subcategory.category_id) GROUP BY category.name


That should give you the whole list. If you need just one item, just add a WHERE...

Reply With Quote
  #7  
Old July 10th, 2003, 04:54 PM
kupilot kupilot is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 8 kupilot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 sec
Reputation Power: 0
Thanks a lot guys , that worked just fine , i was wondering when this Join and Left Join would be usefull

Just a correction the SELECT statement is

SELECT category.name, COUNT(subcat.subid) AS totsub
FROM category
LEFT JOIN subcat ON (category.id = subcat.catid)
GROUP BY category.name

Last edited by kupilot : July 10th, 2003 at 05:14 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > displaying a category total subcategories


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.


© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek