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

Reply via email to