0

I have a table name Agent where storing all the data, and each record are chain to other record, like a tree, How should i retrieve all the LEAF NODE in a SINGLE TABLE with a ROOT NODE ID or a AGENT_ID?

Agent_ID | Agent_name | Recruit_agent_ID
----------------------------------------
1        | Agent1     | Null
----------------------------------------
2        | Agent2     | 1
----------------------------------------
3        | Agent3     | 1
----------------------------------------
4        | Agent4     | 2
----------------------------------------
5        | Agent5     | 3
----------------------------------------

And my desire output should like

ID       | Name       | Parent
----------------------------------------
2        | Agent2     | 1
----------------------------------------
3        | Agent3     | 1
----------------------------------------
4        | Agent4     | 2
----------------------------------------
5        | Agent5     | 3
----------------------------------------

How can i retrieve all leaf when the given Agent_ID = 1?

1000111
  • 13,169
  • 2
  • 28
  • 37
CWei
  • 1
  • 1
  • [MySQL: How to find leaves in specific node](http://stackoverflow.com/questions/12796113/mysql-how-to-find-leaves-in-specific-node). Or use a data model for trees that works in databases, e.g. [Managing Hierarchical Data in MySQL](http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) – Solarflare Sep 17 '16 at 08:48
  • @Solarflare thank you! your reference solve my problem!! – CWei Sep 26 '16 at 08:54

0 Answers0