-1

i am using my sql data base i have table with following data and table name is account

id       name      parent-id
 1       aaa          null
 2       bbb          1
 3       ccc          2
 4       ddd          1
 5       eee          3

i would like to display as follows

  names
   aaa
     bbb
        ccc
           eee
     ddd

can any one help me i have tried a lot but i doesn't get exact requirement please help me

ArunKumar
  • 1
  • 2

2 Answers2

1

There are quite a few sites and pages (1, 2 and 3 for a small sample) which contain material of interest in this area - trees are, after all, a natural way (to a human at least) of thinking about and storing data.

Sadly, MySQL is probably one of the worst databases (certainly of the majors) when it comes to dealing with trees - not CTEs (WITH clause), no Window functions (OVER clause).

Due to the lack of these constructs, it is impossible to produce the kind of output that you require using pure SQL in MySQL. However (!!), it is possible to do this using stored procedures and an excellent demo (with code) of how to do what you want is available here.

Vérace
  • 30,923
  • 9
  • 73
  • 85
-2

This is possible since MySQL 8 using "with recursive".

See the docs: https://dev.mysql.com/doc/refman/8.0/en/with.html

Or a a imho good tutorial on this topic: https://www.mysqltutorial.org/mysql-adjacency-list-tree/

Wonno
  • 1