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, 01: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: < 1 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, 05: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: < 1 sec
Reputation Power: 0
Still no ideas

Reply With Quote
  #3  
Old July 8th, 2003, 08: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: 7
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, 10: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: < 1 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, 11: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: 7
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, 11: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: 7
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, 05: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: < 1 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 06: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


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT