Hi, There are of course several ways of doing it !! I had to do this kind of thing few weeks ago, and I think the easier way of doing it is use a treepath schema in a field of your table
+----+--------+-------------------+ | id | treepath | name | +----+--------+-------------------+ | 1 | 0 - 0 - 1 | parent 1 | | 2 | 0 - 1 - 0 | parent 2 | +----+--------+-------------------+ This case : 0 - 1 - 0 : First sub directory file n° 0 0 - 5 - 1 : Fifth sub directory file n°1 0 path 0 0 path 0-0 1 path 0-1 0 path 0-1-0 1 path 0-1-1 2 path 0-2 1 path 1 0 path 1-0 5 path 1-5 etc... It more simple, more scalable, and it is (I guess!!) the simplest way for sorting results as a tree over a HTML format! The way you store data in your DB depends on the way you output data too!!!! so it depends on your client! Hope It helps... -----Message d'origine----- De : Sagi Bashari [mailto:[EMAIL PROTECTED]] Envoy? : jeudi 23 mai 2002 16:16 ? : [EMAIL PROTECTED] Objet : building tree view in mysql? Hi, I'm trying to build a simple categories tree in MySQL. Say I have the following table: CREATE TABLE test ( id int(10) unsigned NOT NULL auto_increment, parent int(10) unsigned NOT NULL default '0', name tinytext NOT NULL, PRIMARY KEY (id), KEY parent (parent) ) TYPE=MyISAM; +----+--------+-------------------+ | id | parent | name | +----+--------+-------------------+ | 1 | 0 | parent 1 | | 2 | 0 | parent 2 | | 3 | 0 | parent 3 | | 4 | 1 | sub 1 of parent 1 | | 5 | 1 | sub 2 of parent 1 | | 6 | 2 | sub 1 of parent 2 | +----+--------+-------------------+ I would like to get all the childs under their parent, like this: +----+--------+-------------------+ | id | parent | name | +----+--------+-------------------+ | 1 | 0 | parent 1 | | 4 | 1 | sub 1 of parent 1 | | 5 | 1 | sub 2 of parent 1 | | 2 | 0 | parent 2 | | 6 | 2 | sub 1 of parent 2 | | 3 | 0 | parent 3 | +----+--------+-------------------+ How can I get MySQL to sort it like that? I tried to use group/order by, but I cannot get it to sort it this way.. Any ideas? Thanks, Sagi --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php