Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Infinite Category Depth

Author  Topic 

jon3k
Starting Member

49 Posts

Posted - 2005-02-04 : 18:45:50
I've setup a table called category:

id - int, primary key, yadda yadda
parent_id = int
depth = int
cattitle = varchar(50)

And I've written a query that will take each record, and via a reflexive relationship, join each record to its parent:

SELECT category.cattitle, subcat.cattitle FROM category INNER JOIN category subcat ON subcat.parent_id = category.id

And this all works fine and dandy, except that there's no useful and efficient way to output the results. It simply looks at each record and joins it to its parent.

What would be the proper grouping/ordering to get a nice clean outputtable (is that a word?) result set?

I'd like
Category1
--SubCat1
----SubSubCat1
Category2
--SubCat2
----SubSubCat2
----SubSubCat2
Category3
--SubCat3
--SubCat3

Etc, etc.

Anyone ever pulled this off before? Is there a better way to do this? I'd really like to do it with one query, and output it in a single pass, without having to loop over the result set several times.

(mysql and php by the way).

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-02-04 : 18:52:02
quote:
Originally posted by jon3k


(mysql and php by the way).



I would post your question in the mysql forum over at dbforums.com then as any solution provided here would be for SQL Server.

Tara
Go to Top of Page

jon3k
Starting Member

49 Posts

Posted - 2005-02-04 : 18:57:31
I'll try dbforums also, thanks.
Go to Top of Page

arpp
Yak Posting Veteran

61 Posts

Posted - 2005-02-06 : 12:06:42
Look at this posting,It might help you out.
http://www.sqlteam.com/item.asp?ItemID=8866
Go to Top of Page
   

- Advertisement -