MySQL Select parents and children -


i'm having troubles mysql query. kind me out?

i have table:

create table `time` (   `id` int(16) not null,   `user` int(16) not null,   `activity` int(16) not null,   `parent` int(16) default null,   `time` time not null,   `decimaltime` float not null,   `date` date not null ); 

time.parent item in time table. want select time items, , children should listed directly after parent listed.

so example:

id         parent 1          null 2          null 3          null 4          2 5          1 

should listed following:

id         parent 1          null 5          1 2          null 4          2 3          null 

so far tried this:

select a.title, parent.user, parent.activity, parent.time, parent.decimaltime, parent.date, child.parent, case      when (child.parent not null) child.id     else parent.id end id time parent      left join time child       on child.parent = parent.id      left join activity      on child.activity = a.id or parent.activity = a.id parent.parent null order parent.id, child.id 

the problem can't give out correct ids. this happens when run command.

could point out mistake is, please?

thanks in advance.


Comments