How to query graph/hierarchical data in mysql

巧了我就是萌 提交于 2019-11-29 05:20:00
dev_etter

The other answers are correct, there is no recursive functionality in mysql. This link describes how to approach hierarchies in mysql: http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/

I found the link on two other SO questions: How to do MySQL Looped Join which tests if results are complete? and Hierarchical Data in MySql .

MySQL does not support recursive queries. But if you can use a Nested Set which simplifies fetching subtrees.

You cannot do it with mysql because mysql doesn't support recursive functions but if you doesn't need unlimited depth you can use self joins to get the tree. Otherwise you need a recursive function in a programming language or you can use a nested-set which makes it easier to pick subtrees.

Andreas Rehm

Basically this will not work!

You need to have a definition for root nodes. And by the way your "tree" doesn't match your hierary definition.

This would be a better approach:

parent  child
-       A
A       B
A       C
C       D
-       E

The minus parent entries are root nodes. And this reflects your tree.

Sql does not offer hierarchy queries. But you can iterate whith a trick:

A good explanation can be found here:

I would try to find root nodes and use a recursion to concat the text.

root nodes will be:

SELECT * FRON objects WHERE parent = '-'

If u have a key (varchar)m u can sort by it. The key values would look like:

001
001.001
001.001.001
001.001.002
001.001.003
002
002.001
002.001
003
003.001
...
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!