For example, I have a table like this:
----------------------
| id | Name | Parent |
......................
| 1 | Joe | '' |
| 2 | Alice| '' |
| 3 | Manny| '' |
| 4 | kid1 | 1 |
| 5 | kid2 | 1 |
| 6 | kid3 | 3 |
and I want to display it in a hierarchy manner like this:
| id | Name | Parent |
......................
| 1 | Joe | '' |
| 4 | kid1 | 1 |
| 5 | kid2 | 1 |
| 2 | Alice| '' |
| 3 | Manny| '' |
| 6 | kid3 | 3 |
Can I do it using only SQL commands?
Thank you so much guys. I'm new to stackoverflow, yet I'm already amazed by how fast you have answered my question.
@amar duplantier, Thanks for the link it solved my problem!! I can't find that thread when I searched before. I'm sorry I didn't provide enough information for my question.
here's the code I use based on amar's link:
select *
from Table a
order by
case
when Parent = ''
then id
else (
select id
from Table parent
where parent.id = a.Parent
)
end
DESC