如何在MySQL表中表示树结构 [英] How do I represent a Tree Structure in a mySQL table

查看:270
本文介绍了如何在MySQL表中表示树结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

树具有以下特征:

  1. 每个节点可以有多个父级和多个子级.
  2. 节点的父级节点可以具有不同的深度.

示例

我正在尝试表示一种如下的类别结构:

I am trying to represent a category structure such as the following:

台式机和移动应用程序

桌面和移动应用程序-> Android应用程序

Desktop and Mobile Applications->Android Apps

桌面和移动应用程序-> Android应用程序->游戏

Desktop and Mobile Applications->Android Apps->Games

桌面和移动应用程序-> Android应用程序->游戏->动作

Desktop and Mobile Applications->Android Apps->Games->Action

桌面和移动应用程序->游戏

Desktop and Mobile Applications->Games

桌面和移动应用程序->游戏->动作

Desktop and Mobile Applications->Games->Action

桌面和移动应用程序->游戏->冒险

Desktop and Mobile Applications->Games->Adventure

桌面应用程序

桌面应用程序->游戏

桌面应用程序->游戏->动作

Desktop Applications->Games->Action

桌面应用程序->游戏->冒险

Desktop Applications->Games->Adventure

iPhone应用程序

桌面应用程序->游戏

桌面应用程序->游戏->动作

Desktop Applications->Games->Action

桌面应用程序->游戏->冒险

Desktop Applications->Games->Adventure

尝试使用嵌套集算法,最后得到多个具有不同类别ID和不同深度的游戏"类别.

Tried using the Nested Set Algorithm and I end up with multiple "Games" categories with different categoryIDs and at different depths.

任何帮助,将不胜感激.

Any help with this will be much appreciated.

推荐答案

这是一个非常有用的教程 http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

Here is a very usefull tutorial http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

这篇关于如何在MySQL表中表示树结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆