0

I have a table like this:

tb_objs
-------
obj_id int(10) unsigned NOT NULL AUTO_INCREMENT
obj_name varchar(45) NOT NULL
obj_parent int(10) unsigned DEFAULT NULL

It`s something like a ordered list... The first element has the 'obj_parent' = null, and the second elemente has 'obj_parent' = id of first element etc etc etc...

So, i want select all elements ordered by obj_parent... I dont know if i am cleary in what a i want... But the result has to be like this:

obj_id   |   obj_name   |   obj_parent
  3      |    first     |      null
  9      |    second    |       3
  1      |    third     |       9
  2      |    fourth    |       1
  4      |    fifth     |       2
  ...
  etc   etc    etc   etc

If you take a look... The object 'child' is always after his parent... It`s like a C++ ordered list...

obj_parent
obj_id        <------   object_parent
                        obj_id           <------ obj_parent
                                                 obj_id
giordanolima
  • 1,190
  • 1
  • 12
  • 21
  • I don't think I understand, maybe some more data would help. What you show looks like it could be solved by a ORDER BY obj_parent. Is your problem that you want NULL 1st and it's being sorted last? – Barbara Laird Aug 12 '14 at 19:53
  • 2
    Check this: http://stackoverflow.com/questions/20215744/mysql-hierarchical-recursive-query – EIZ Aug 12 '14 at 20:03
  • 1
    MySQL really has no built in support for hierarchical/recursive queries. You either need to do this in a stored procedure. Or, store the entire parent path to the top parent in each row. – Gordon Linoff Aug 12 '14 at 20:09
  • Tks a lot... You`ve been useful... – giordanolima Aug 12 '14 at 20:13

0 Answers0